如何记住选中的行并在UITableView中默认重新加载后维护它们?

use*_*567 29 iphone objective-c ios4 ios

如何设置默认选择的行?我想选择一行,获取所选行的数量,indexpath.row然后重新加载表并选择所选行.有谁可以帮助我吗?我可以在方法中获取所选行 - (IBAction)segmentedControlIndexChanged吗?不在didSelectRowAtIndexPath

这是我的代码,如果它可以帮助您理解

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Sea*_*ell 64

NSIndexPath *indexPath = [tableView indexPathForSelectedRow];
Run Code Online (Sandbox Code Playgroud)

将为您NSIndexPath提供当前所选行.然后你可以做

[tableView selectRowAtIndexPath:indexPath 
                       animated:NO 
                 scrollPosition:UITableViewScrollPositionMiddle];
Run Code Online (Sandbox Code Playgroud)

稍后选择该行(并在屏幕中间显示).

  • 另外,确保你的`UITableViewController`上的`clearsSelectionOnViewWillAppear`设置为'NO`,否则它将在视图出现时自动取消选择该行.这让我困扰了好几个小时. (26认同)
  • 我在`viewDidAppear`中调用它后,这适用于iOS 8 (2认同)

xls*_*rlx 30

我有同样的问题,这就是我做的方式:

在你的tableViewController,在这个功能中添加它

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

    }

}
Run Code Online (Sandbox Code Playgroud)

  • @refdev如果表格非常大,我不确定这种方法是否会起作用,因为并非所有单元格都可以一次创建(只有前50个左右),其他单元格则需要.如果你的行低于初始限制,除非用户向下滚动,否则永远不会执行`if`中的代码. (3认同)

小智 9

这样做:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)

其中row是要选择的单元格编号(从0开始...),section是单元格出现的部分.


mah*_*udz 5

这是您在UITableView中预选行的方法:

[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition: UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)

将indexPath设置为要选择的行,然后按上述方法调用方法.