初始选择的 UITableViewCell 集 [英] UITableViewCell Set selected initially
问题描述
我有这样的情况,在 iPad 应用程序中,我的 master controller
有列表,details 控制器 有它的细节,典型的 UISplitViewController
模式.我想要实现的是,最初应该选择我的第一行,然后我想为用户提供选择选择.我正在使用单元格的 setSelected
并在 didSelectRowAtIndexPath
方法中删除这样的选择.
Hi there I have situation where in an iPad app my master controller
has list and details controller have its details , a typical UISplitViewController
pattern.
What I want to achieve is , my first row should be initially selected and later I want to give selection choice to user.
I am using cell's setSelected
and in didSelectRowAtIndexPath
method removing selection like this.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSIndexPath* path = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:path];
[cell setSelected:NO];
}
对于初始单元格,但在那之后我没有选择单元格请帮帮我.
for initial cell but my none cell is getting selected after that please help me out.
推荐答案
试试这个
NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:tableViewList didSelectRowAtIndexPath:selectedCellIndexPath];
[tableViewList selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
或
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (/* should be selected */) {
[cell setSelected:YES animated:NO];
}
}
Swift 4 版本是
Swift 4 version is
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
func shouldSelect() -> Bool {
// Some checks
}
if shouldSelect() {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
}
这篇关于初始选择的 UITableViewCell 集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!