【发布时间】:2009-08-24 16:43:43
【问题描述】:
我创建了一个UITableView,并希望在加载视图时显示一个特定的UITableViewCell(蓝色)。
【问题讨论】:
标签: iphone ios cocoa-touch uitableview
我创建了一个UITableView,并希望在加载视图时显示一个特定的UITableViewCell(蓝色)。
【问题讨论】:
标签: iphone ios cocoa-touch uitableview
-(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];
}
我正在使用这种技术从两个UITableViewCells 中选择一个,这样用户就可以知道表格视图下方的UIDatePicker 会影响哪个单元格。当您创建新事件并设置日期时,Apple 在日历应用程序中使用此技术。
【讨论】:
请谨慎使用此方法,因为以这种方式选择行是Apple suggests against 为显示“已选择”状态所做的事情。
请考虑将单元格的accessoryType 属性设置为UITableViewCellAccessoryCheckmark 之类的值。
【讨论】:
你应该把它放在viewWillAppear。
[myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:0];
如果你尝试在cellForRowAtIndexPath:中选择它,那么它将不会采用所需的样式。
【讨论】:
一定要小心。我相信您有充分的理由,但请仔细查看 Apple 提供的人机界面指南文档。应用程序因未取消选择表行而被拒绝。我鼓励您找到 HIG 的相应部分,并查看 Apple 提供的任何建议。
【讨论】:
使用此代码在表格视图中默认选择单元格,indexPath 可以根据您的需要而变化
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
[theTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];
}
【讨论】:
【讨论】:
有时,当您不在UIViewController 中时,您没有viewWillAppear,有时您以编程方式创建了UITableView。
简单的解决方案是实现这个delegate 方法:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.selectedIndex == indexPath.row) {
cell.selected = YES;
}
}
它在cellForRowAtIndexPath 中不起作用,因为该单元格尚未显示。而setSelected 方法会在这个显示时被调用。
【讨论】: