UITableViewCell Set最初选择

Sag*_* In 38 uitableview uisplitviewcontroller ios

嗨,我有一个情况,在iPad应用程序我的master controller列表和细节控制器有其细节,一个典型的UISplitViewController模式.我想要实现的是,我的第一行应该是最初选择的,之后我想给用户选择选择.我正在使用单元格setSelecteddidSelectRowAtIndexPath方法删除这样的选择.

-(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];
} 
Run Code Online (Sandbox Code Playgroud)

对于初始细胞,但我的无细胞被选中后请帮助我.

Dre*_*w C 83

要使单元格显示为已选中,您必须-setSelected:animated:从内部调用,-tableView:willDisplayCell:forRowAtIndexPath:如下所示:

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

-setSelected从其他任何地方打电话都没有效果.

  • 如果您可以添加以下行,那么这个答案会很棒[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 这允许用户取消选择单元格.如果缺少此行,则无法取消选择该行. (26认同)
  • 但是如果你想使用[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; 你必须将它粘贴在[tableView:cellForRowAtIndexPath:]中,因为在[tableView:weillDisplayCell:]中它不起作用. (4认同)
  • @cateof的评论应该添加到答案中!很重要. (2认同)
  • 这个答案是错误的,因为它只会调用动画块,而不会实际将其标记为选定状态 - 这将破坏“didDeselectRowAt”。使用 tableView.selectRow(at:indexPath,animated:false,scrollPosition:.none)` (2认同)

Rav*_*ran 55

试试这个

NSIndexPath* selectedCellIndexPath= [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:tableViewList didSelectRowAtIndexPath:selectedCellIndexPath];
[tableViewList selectRowAtIndexPath:selectedCellIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)

要么

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


Pet*_*inz 40

Swift 4:最初选择单元格

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: UITableView.ScrollPosition.none)
        }
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是选定的答案,因为它在选择另一行时保留选择.也许在if语句中添加一个检查,以确保它只选择行,如果它不是当前的; y选择了. (3认同)
  • 我同意道格拉斯。这应该标记为正确答案。在过去的几个小时中,我一直在寻找解决方案,这可以解决我的问题。 (2认同)

小智 10

我不知道你期待这个答案.默认情况下,如果要在没有手动选择的情况下选择tableView中的第一行,只需启动这样的didSelectRowAtIndexPath方法即可

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


Lal*_*hna 7

最佳选择

斯威夫特 5

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if shouldSelectThisRow {
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    } else {
        tableView.deselectRow(at: indexPath, animated: false)
    }
}
Run Code Online (Sandbox Code Playgroud)


Ash*_*shu 5

它也将有助于POP导航

.h文件,

NSIndexPath *defaultSelectedCell
Run Code Online (Sandbox Code Playgroud)

.m文件

把这段代码放进去 ViewDidLoad

defaultSelectedCell= [NSIndexPath indexPathForRow:0 inSection:0];
Run Code Online (Sandbox Code Playgroud)

viewDidAppear

[self.tableView selectRowAtIndexPath:defaultSelectedCell animated:NO  scrollPosition:UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)

这将有助于POP,将此代码放入 viewWillAppear

[self.catTableView selectRowAtIndexPath:defaultSelectedCell animated:NO scrollPosition:UITableViewScrollPositionNone];
Run Code Online (Sandbox Code Playgroud)

tableView didSelectRowAtIndexPath方法中,

defaultSelectedCell = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
Run Code Online (Sandbox Code Playgroud)

这有助于我第一次加载或弹出导航.