Pawan Joshi
Pawan Joshi

Reputation: 1579

How to keep track of selected indexes (in array) in a UITableViewController Which contains another UITableView as its Cells

with the help of THIS Tutorial . . . .

I made a UITableViewController (for reference I can call it MainTableVC) , which has single row in each of its section and I made every cell of this UITableViewController(MainTableVC) a UITableView (for reference I can call it UITableViewOfMainTableVCell) with many cells.

Now I rotated Cell/rows of UITableViewOfMainTableVCell {* its a UITableView not a UITableViewCell }, so that i could scroll cells of UITableViewOfMainTableVCell horizontally.

now my UI looks like this, I have a table with 10 section(say), each section has one row/cell and in this row/cell i have several rows/cells which can be scrolled horizontally

I also enabled allowMultipleSelections in custom cells of UITableViewOfMainTableVCell

Now I wanted to keep track of selected indexes of selected rows so I took an NSArray and saved indexPaths in it with this delegate method of UITableViewOfManiTableVCcells

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *selectedIndexesOfOneRow = [NSMutableArray arrayWithArray:[tableView indexPathsForSelectedRows]];
NSLog(@"number of selected Indexes = %d",[selectedIndexesOfOneRow count]);
}

-(void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *selectedIndexesOfOneRow= [NSMutableArray arrayWithArray:[tableView indexPathsForSelectedRows]];
NSLog(@"number of selected Indexes = %d",[selectedIndexesOfOneRow count]);
}

MY PROBLEM :-

suppose if I select 4 cells/rows in a row/cell of MainTableVC the NSMutableArray *selectedIndexesOfOneRow has the right count BUT if I select cells of another a row in another section of MainTableVC with even keeping previous cells selected the count starts from beginning

the problem is with indexPathsForSelectedRows, it only keeps track of Selected Rows of single UITableView, since i have separate UITableViews for each Row of Each Section of MainTableVC NSMutableArray always gets reset when select cells from different row

MY QUESTION

since every Row/Cell calls the same UITableView delegate method, how will I prevent it, I want to store all the selected indexes of cells in whole UI

I could make an array of array to store indexes of different different indexes but how would i get those differently

"HINT" I could do somthing like this, in my MainTableVC, Since i have a tableView inside Cell of MainTableVC i can put an NSArray *array = [cell.tableView indexPathsOfSelectedIndex], but again in which delegate method of MainTableVC should i put this, and also i have checked, Since i am not selecting any whole row of MainTableVC(UITableViewController), its delegate methods didSelectRowAtIndexPath and didDeSelectRowAtIndexPath never get called

Upvotes: 1

Views: 1350

Answers (2)

Mannam Brahmaiah
Mannam Brahmaiah

Reputation: 2283

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 // NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);
NSString *listOfFacebookIDs = @"";
NSArray *indexPathArray = [self.mTableView 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 = [dataArray objectAtIndex:index.row];
    if([indexPathArray lastObject]!=index){
        listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[fbID stringByAppendingString:@", "]];
    }else{
        listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:fbID];
    }
}
  NSLog(@"Your comma separated string is %@",listOfFacebookIDs);
}


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"user de-selected %@",[dataArray objectAtIndex:indexPath.row]);
}

enter image description here

Upvotes: -1

Rajneesh071
Rajneesh071

Reputation: 31091

You can assing tag to your tableView, then check tag to distinguish all tableView.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==1) {
        //Do what ever you want with table having tab =1
    }else if (tableView.tag==2) {
        //Do what ever you want with table having tag =2
    }
}

Upvotes: 1

Related Questions