How to get the values of selected cells of a UITableView in one string

£可爱£侵袭症+ 提交于 2019-12-12 02:46:51

问题


i'm pretty sure this is really simple. But i can't get to make this work. I have a UITableView where i display dynamically a list of facebook friends, thanks to their FBID. Basically, i would like to return the FBIDs of the friends i selected, in one string separated with commas. If possible, in a IBAction, so i can pass them into parameters of a php. For example, let's pretend i have a list of 4 friends, A B C D, and their ids are 1,2,3,4. If i select A and C, i would like to have a string which says 1,3.

All i managed to do is to display the id of the friend i select, one at a time. Here's my code :

-(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);


}

Thank you in advance, i'm sure this is not complicated.


回答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
}



回答2:


The problem is that you are not using the information in indexPathsForSelectedRows. That is telling you all the selected rows. Instead, you are just looking at indexPath.row, which is the one row most recently selected.

What you need to do is cycle through indexPathsForSelectedRows and gather up the info for every row that is currently selected (I'm assuming you've enabled multiple selection here).




回答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.


来源:https://stackoverflow.com/questions/16492849/how-to-get-the-values-of-selected-cells-of-a-uitableview-in-one-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!