user567
user567

Reputation: 3852

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

Upvotes: 30

Views: 56518

Answers (6)

karthikc217
karthikc217

Reputation: 11

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.

Upvotes: 0

Mecki
Mecki

Reputation: 132929

I realize that this question is rather old (2011), but just to also post an up-to-date answer, in case someone (like me) searches for this problem and stumbles across this question:

With iOS 9, Apple has improved UITableView a lot. To make sure that focus is not lost when a table is reloaded, just do this:

tableView.remembersLastFocusedIndexPath = Yes;

and it will "magically work".

Even better, implement

- (NSIndexPath *)indexPathForPreferredFocusedViewInTableView:(UITableView *)tableView

in the UITableViewDelegate to let the table know which row you want selected when it is drawn to screen for the very first time.

Before iOS 9, I'd usually just write my own reloadData method, that gets the currently selected row, reloads the table and then selects it again. Either in the UITableViewController or if I needed that to work with a table where I couldn't prevent code calling reloadData directly, I subclassed UITableView and overrode reloadData (hey, delegation is the preferred way, but sometimes you just have to subclass and override, even in iOS).

Upvotes: 4

xlsmearlx
xlsmearlx

Reputation: 638

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
        ];

    }

}

Upvotes: 31

Nuno Costa
Nuno Costa

Reputation: 423

This will do it:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];

Where row is the cell number you want to select (starts 0...), and section is the section where the cell appears.

Upvotes: 9

Seamus Campbell
Seamus Campbell

Reputation: 17906

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.

Upvotes: 64

mahboudz
mahboudz

Reputation: 39376

This is how you preselect a row in a UITableView:

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

Set the indexPath to be the row you want selected and then make the method call as above.

Upvotes: 5

Related Questions