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
How do I select a UITableViewCell by default?
提问by user119857
I have created a UITableView
and would like a specific UITableViewCell
to 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 UITableViewCells
so the users knows which cell a UIDatePicker
below 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 accessoryType
property 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, indexPath
can 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
回答by Damien Romito
Sometimes, when you're not in a UIViewController
, you have no viewWillAppear
, and sometimes, you created your UITableView
programmatically.
有时,当您不在 中时UIViewController
,您没有viewWillAppear
,有时,您以UITableView
编程方式创建了您的。
The easy solution is to implement this delegate
method:
简单的解决方案是实现这个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 cellForRowAtIndexPath
because the cell is not yet displayed. and the setSelected
method is called just when this one is displayed.
它在 中不起作用,cellForRowAtIndexPath
因为该单元格尚未显示。并且该setSelected
方法仅在显示该方法时调用。