-->

如何获得在一个串一个UITableView的选定单元格的值(How to get the value

2019-10-29 19:49发布

我敢肯定,这是非常简单的。 但我不能去,使这项工作。 我有一个UITableView其中i动态显示的Facebook好友列表,感谢他们FBID。 基本上,我想回到我选择朋友的FBIDs,用逗号分隔的一个字符串。 如果可能的话,在一个IBAction为,所以我可以将它们传递到一个PHP的参数。 例如,让我们假设我有4个朋友,ABCD列表,以及它们的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);


}

谢谢你在前进,我敢肯定,这并不复杂。

Answer 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
}


Answer 2:

问题是,你没有使用信息indexPathsForSelectedRows 。 这是告诉你所有选定行。 相反,你只是在看indexPath.row ,这是最近选择了一行

什么,你需要做的是循环indexPathsForSelectedRows并收集起来用于当前所选的每一行(我假设你已经启用了这里多选)的信息。



Answer 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.


文章来源: How to get the values of selected cells of a UITableView in one string