ios 默认情况下如何选择 UITableViewCell?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1323474/
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 16:19:25  来源:igfitidea点击:

How do I select a UITableViewCell by default?

iphoneioscocoa-touchuitableview

提问by user119857

I have created a UITableViewand would like a specific UITableViewCellto appear selected (blue) when the view is loaded.

我创建了一个UITableView并希望UITableViewCell在加载视图时显示选定的特定对象(蓝色)。

回答by Hafthor

-(void)viewWillAppear:(BOOL)animated {
    // assuming you had the table view wired to IBOutlet myTableView
    // and that you wanted to select the first item in the first section
    [myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
                             animated:NO 
                       scrollPosition:UITableViewScrollPositionTop];
}

I'm using this technique to select one of two UITableViewCellsso the users knows which cell a UIDatePickerbelow the table view it will effect. This technique is used by Apple in the calendar app when you create a new event and set the dates.

我正在使用这种技术选择两个中的一个,UITableViewCells以便用户知道UIDatePicker它将影响表格视图下方的哪个单元格。当您创建新事件并设置日期时,Apple 在日历应用程序中使用此技术。

回答by Shaggy Frog

Be judicious using this method, as selecting the row in this way is something Apple suggests againstdoing to show a "chosen" state.

使用这种方法要明智,因为以这种方式选择行是Apple 建议不要做的事情来显示“选择”状态。

Instead, consider setting the cell's accessoryTypeproperty to something like UITableViewCellAccessoryCheckmark.

相反,请考虑将单元格的accessoryType属性设置为类似 UITableViewCellAccessoryCheckmark.

回答by Pragnesh Ambani

You should put it in viewWillAppear.

你应该把它放进去viewWillAppear

[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] 
                         animated:NO 
                   scrollPosition:0];

If you try to select it in cellForRowAtIndexPath:, then it will not take the required style.

如果您尝试在 中选择它cellForRowAtIndexPath:,那么它将不会采用所需的样式。

回答by Sayali Shinde

Use this code to select cell by default in table view, indexPathcan be vary according to your need

使用此代码在表格视图中默认选择单元格,indexPath可以根据您的需要而变化

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}

回答by marcc

Definitely watch out. I'm sure you have a good reason, but look closely at the Human Interface Guidelines document Apple provides. Apps get rejected for not unselecting table rows. I'd encourage you to find the appropriate section of the HIG and see Apple offers any suggestions.

一定要注意。我相信您有充分的理由,但请仔细查看 Apple 提供的人机界面指南文档。应用程序因未取消选择表行而被拒绝。我鼓励您找到 HIG 的相应部分,并查看 Apple 提供的任何建议。

回答by Daniel

Use the UITableViewCell method

使用 UITableViewCell 方法

- (void)setSelected:(BOOL)selected animated:(BOOL)animated

Info here.

信息在这里

回答by Damien Romito

Sometimes, when you're not in a UIViewController, you have no viewWillAppear, and sometimes, you created your UITableViewprogrammatically.

有时,当您不在 中时UIViewController,您没有viewWillAppear,有时,您以UITableView编程方式创建了您的。

The easy solution is to implement this delegatemethod:

简单的解决方案是实现这个delegate方法:

- (void)tableView:(UITableView *)tableView 
        willDisplayCell:(UITableViewCell *)cell 
        forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (self.selectedIndex == indexPath.row) {
        cell.selected = YES;
    }
}

It's does not work in the cellForRowAtIndexPathbecause the cell is not yet displayed. and the setSelectedmethod is called just when this one is displayed.

它在 中不起作用,cellForRowAtIndexPath因为该单元格尚未显示。并且该setSelected方法仅在显示该方法时调用。