如何在一个字符串中获取 UITableView 的选定单元格的值

Posted

技术标签:

【中文标题】如何在一个字符串中获取 UITableView 的选定单元格的值【英文标题】:How to get the values of selected cells of a UITableView in one string 【发布时间】:2013-05-11 01:21:57 【问题描述】:

我很确定这真的很简单。但我无法完成这项工作。我有一个 UITableView,我在其中动态显示 facebook 朋友的列表,这要归功于他们的 FBID。基本上,我想在一个用逗号分隔的字符串中返回我选择的朋友的 FBID。如果可能,在 IBAction 中,我可以将它们传递给 php.ini 的参数。 例如,假设我有 4 个朋友 A B C D 的列表,他们的 id 是 1,2,3,4。 如果我选择 A 和 C,我想要一个字符串,上面写着 1,3。

我所做的只是显示我选择的朋友的 ID,一次一个。 这是我的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

rowcount = [[tableView indexPathsForSelectedRows] count];
indexer = [idFriends objectAtIndex:indexPath.row];
aapell = [NSString stringWithFormat:@"%@", indexer];

NSMutableString * arrayIds = [[NSMutableString alloc] init];
[arrayIds appendString:aapell];

NSLog(@"ids: %@", arrayIds);



提前谢谢你,我相信这并不复杂。

【问题讨论】:

【参考方案1】:
-(IBAction)gatherFBIds

    NSString *listOfFacebookIDs = @"";
    NSArray *indexPathArray = [self.tableView indexPathsForSelectedRows];
    for(NSIndexPath *index in indexPathArray)
    
        //Assuming that 'idFriends' is an array you've made containing the id's (and in the same order as your tableview)
        //Otherwise embed the ID as a property of a custom tableViewCell and access directly from the cell
        //Also assuming you only have one section inside your tableview
        NSString *fbID = [idFriends objectAtIndex:index.row];
        if([indexPathArray lastObject]!=index)
        
            listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[fbID stringByAppendingString:@", "]];
        
        else
        
            listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:fbID];

        
    

    NSLog(@"Your comma separated string is %@",listOfFacebookIDs);
    //Now pass this list to wherever you'd like

【讨论】:

设法使它工作!感谢一百万这是解决方法: for(NSIndexPath *index in indexPathArray) NSString *fbID = [idFriends objectAtIndex:index.row]; listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[NSString stringWithFormat:@"%@,",fbID]]; if ( [listOfFacebookIDs 长度] > 0) listOfFacebookIDs = [listOfFacebookIDs substringToIndex:[listOfFacebookIDs 长度] - 1]; NSLog(@"你的逗号分隔字符串是 %@",listOfFacebookIDs); //现在将此列表传递到您想要的任何地方【参考方案2】:

问题是您没有使用indexPathsForSelectedRows 中的信息。这就是告诉您 all 选定的行。相反,您只是查看indexPath.row,这是最近最近选择的一行

您需要做的是循环浏览indexPathsForSelectedRows 并收集当前选择的每一行的信息(我假设您已在此处启用多项选择)。

【讨论】:

【参考方案3】:
    Use the following code to get the selected rows in a table view. :)     

    NSArray *selectedRows=[tableView indexPathsForSelectedRows];
            NSMutableArray *rownumberArray=[[NSMutableArray alloc]init];
            for (int i=0; i<selectedRows.count; i++) 
                NSIndexPath *indexPath = [selectedRows objectAtIndex:i];
                NSInteger row = indexPath.row;
                NSNumber *number = [NSNumber numberWithInteger:row];
                [rownumberArray addObject:number];
            

  //  rownumberArray contains the row numbers of selected cells.

【讨论】:

以上是关于如何在一个字符串中获取 UITableView 的选定单元格的值的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Swift3 在​​第二个 UITableView 中获取第一个 UITableView 的索引 path.row

如何在iOS中的UITableView的最后一行添加一行字符串?

如何在 UITableView 中获取 UIPickerView 值

在 Swift 中,如何从 UITableView 中获取对象 id 并根据对象 id 删除对象?

如何在自定义 UITableView 中获取文本字段

如何在 UITableview 中使用 UISearchBar 在本地 JSON 中获取数据?