Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

ios - 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;
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...