Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remember rows selected and maintain them after reload by default in UITableView?

How can I set a row selected by default? I want to select a row , take the number of selected row with indexpath.row and then reload the table and select the row what was selected. Can someone help me please? Can I get the selected row in a method -(IBAction) segmentedControlIndexChanged? Not in didSelectRowAtIndexPath?

This is my code if it can help you to understand

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString *CellIdentifier = @"Cell";      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];     if (cell == nil) {         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];     }     //lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 100, 20) ];      // Configure the cell...     NSString *depdateChoosed=[deparatureDates objectAtIndex:depSelectedIndice];     NSString *comeateChoosed=[deparatureDates objectAtIndex:comSelectedIndice];     NSString *choosedDate =[NSString stringWithFormat:@"%@%@",depdateChoosed, comeateChoosed];     NSLog(@"date choisi %@ ",choosedDate);      if(segment.selectedSegmentIndex==0)     {         cell.textLabel.text =[deparatureDates objectAtIndex:indexPath.row];             }     else if(segment.selectedSegmentIndex==1) {          //cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row];         //cell.textLabel.text=[goingBackDates objectAtIndex:indexPath.row];         cell.textLabel.text =[goingBackDates objectAtIndex:indexPath.row];     }     return cell; }   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     NSLog(@" champ séléctionner : %d ",indexPath.row);      if(segment.selectedSegmentIndex==0)     {         depSelectedIndice=indexPath.row;       }     else if(segment.selectedSegmentIndex==1) {         comSelectedIndice=indexPath.row;     }     //[tableView reloadData]; }  -(IBAction) segmentedControlIndexChanged {     int i;     switch (self.segment.selectedSegmentIndex)      {         case 0:             i=0;              [tableView reloadData];             break;         case 1:             i=1;             NSLog(@"dexieme segment selectionner");             [tableView reloadData];         default:             break;     } } 
like image 405
user567 Avatar asked May 24 '11 23:05

user567


People also ask

How do you get the indexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What does indexPath row return?

So to start with the indexPath. row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.

What is indexPath Uitableview?

IndexPath contains information about which row in which section the function is asking about. Base on this numbers you are configuring the cell to display the data for given row.

What is indexPath in Tableview Swift?

indexPath(for:)Returns an index path that represents the row and section of a specified table-view cell.


2 Answers

NSIndexPath *indexPath = [tableView indexPathForSelectedRow]; 

will give you the NSIndexPath for the current selected row. You can then do

[tableView selectRowAtIndexPath:indexPath                         animated:NO                   scrollPosition:UITableViewScrollPositionMiddle]; 

to select that row (and show it in the middle of the screen) later.

like image 115
Seamus Campbell Avatar answered Oct 05 '22 04:10

Seamus Campbell


I had the same question, and this is how I did it:

In your tableViewController, add this in this function

(UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {         if(indexPath.row == 'the row you want to select')     {         [tableView              selectRowAtIndexPath:indexPath              animated:TRUE              scrollPosition:UITableViewScrollPositionNone         ];          [[tableView delegate]              tableView:tableView              didSelectRowAtIndexPath:indexPath         ];      }  } 
like image 44
xlsmearlx Avatar answered Oct 05 '22 03:10

xlsmearlx