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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6118071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:04:35  来源:igfitidea点击:

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

iosiphoneobjective-cios4

提问by user567

How can I set a row selected by default? I want to select a row , take the number of selected row with indexpath.rowand 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?

如何设置默认选中的行?我想选择一行,获取所选行的编号,indexpath.row然后重新加载表格并选择所选行。有人能帮助我吗?我可以在方法 -( IBAction) 中获取所选行segmentedControlIndexChanged吗?不在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;
    }
}

回答by Seamus Campbell

NSIndexPath *indexPath = [tableView indexPathForSelectedRow];

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

将为您NSIndexPath提供当前选定行的 。然后你可以做

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

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

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

回答by xlsmearlx

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

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

In your tableViewController, add this in this function

在您的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
        ];

    }

}

回答by Nuno Costa

This will do it:

这将做到:

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

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

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

回答by mahboudz

This is how you preselect a row in a UITableView:

这是在 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.

将 indexPath 设置为您想要选择的行,然后调用上述方法。

回答by Mecki

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:

我意识到这个问题很旧(2011),但只是为了发布一个最新的答案,以防有人(像我一样)搜索这个问题并偶然发现这个问题:

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

在 iOS 9 中,Apple 有了很大的改进UITableView。要确保在重新加载表时不会丢失焦点,只需执行以下操作:

tableView.remembersLastFocusedIndexPath = Yes;

and it will "magically work".

它会“神奇地工作”。

Even better, implement

更好的是,实施

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

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

在第一次UITableViewDelegate将表格绘制到屏幕时,让表格知道您要选择哪一行。

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

在 iOS 9 之前,我通常只编写自己的reloadData方法,获取当前选定的行,重新加载表,然后再次选择它。无论是在UITableViewController或如果我需要的是同一个表,其中我也无法阻止代码调用工作reloadData直接,我子类的UITableView和压倒了reloadData(嘿嘿,代表团的首选方式,但有时你只需要子类覆盖,甚至在iOS版)。

回答by karthikc217

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.

// rownumberArray 包含所选单元格的行号。