سؤال

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.

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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

    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.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top