2013-10-10 117 views
31

嗨,我有情况下,在一个iPad应用程序我的master controller有名单和细节控制器有它的细节,一个典型的UISplitViewController模式。 我想要实现的是,我的第一行应该是最初选择的,后来我想给用户选择选择。 我正在使用单元的setSelecteddidSelectRowAtIndexPath方法删除这样的选择。UITableViewCell设置选择最初

-(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]; 
} 

初始小区但 后我没有细胞得到选择,请帮助我。

+0

你想自动选择您的tableView的第一行? – wesley

回答

41

试试这个

NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0]; 
[self tableView:tableViewList didSelectRowAtIndexPath:selectedCellIndexPath]; 
[tableViewList selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 

OR

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if (/* should be selected */) { 
    [cell setSelected:YES animated:NO]; 
} 
} 
8

我不知道你期待着这个答案。通过,如果你想在你的tableView选择第一行用了手动选择默认,只需启动didSelectRowAtIndexPath方法这样

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

对于细胞出现选择,你必须从-tableView:willDisplayCell:forRowAtIndexPath:内呼叫-setSelected:animated:像这样:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (/* should be selected */) { 
     [cell setSelected:YES animated:NO]; 
    } 
} 

从其他地方调用-setSelected不起作用。

+15

这应该是公认的答案。 – RaffAl

+19

如果你可以添加下面的代码,这个答案会很棒[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];这是允许用户取消选择该单元格。如果缺少该行,则不能取消该行。 – cateof

+3

但是如果你想使用[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];你必须将它粘贴在[tableView:cellForRowAtIndexPath:]中,因为在[tableView:weillDisplayCell:]它不起作用。 –

5

这也将有助于在POP导航

h文件的情况下,

NSIndexPath *defaultSelectedCell 

.m文件

viewDidAppear将这个代码ViewDidLoad

defaultSelectedCell= [NSIndexPath indexPathForRow:0 inSection:0]; 

[self.tableView selectRowAtIndexPath:defaultSelectedCell animated:NO scrollPosition:UITableViewScrollPositionNone]; 

这将有助于当你取出,在tableView didSelectRowAtIndexPath方法将这个代码viewWillAppear

[self.catTableView selectRowAtIndexPath:defaultSelectedCell animated:NO scrollPosition:UITableViewScrollPositionNone]; 

defaultSelectedCell = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; 

这有助于我非常任第一次装载或弹出导航。

19

斯威夫特3:选择单元初始

import UIKit 

class MyViewController: UIViewController, UITableViewDelegate { 
    @IBOutlet weak var tableView: UITableView! 
    ... 

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 

     if /* your code here */ == indexPath.row { 
      tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none) 
     } 
    } 

    ... 
} 
+2

这应该是选择的答案,因为当选择另一行时它会保留选择。也许在if语句中添加一个检查,以确保它只在该行不是最新时才选择该行; y选择tho。 –

+1

我同意道格拉斯。这应该被标记为正确答案。过去几个小时我一直在寻找解决方案,这解决了我的问题。 – user30646

+0

@ user30646 same here!..这应该是正确的答案,因为它帮助我解决了我的问题! – Pangu

相关问题