48

嗨,我有一种情况,在 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];
} 

对于初始单元格,但之后我的单元格没有被选中,请帮助我。

4

12 回答 12

88

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

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

从其他任何地方拨打电话-setSelected无效。

于 2014-08-04T22:15:16.540 回答
63

尝试这个

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 版本是

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)
    }
}
于 2013-10-10T12:44:22.417 回答
48

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)
        }
    }

    ...
}
于 2016-12-28T12:41:25.543 回答
10

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

- (void)viewDidAppear:(BOOL)animated {
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [myTableView selectRowAtIndexPath:indexPath animated:YES  scrollPosition:UITableViewScrollPositionBottom];
}
于 2013-10-10T12:29:00.367 回答
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)
    }
}
于 2019-04-17T07:07:28.080 回答
5

在 POP 导航的情况下,它也会有所帮助

.h 文件,

NSIndexPath *defaultSelectedCell

.m 文件

把这段代码放进去ViewDidLoad

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

viewDidAppear

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

这将在您 POP 时有所帮助,将此代码放入viewWillAppear

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

tableView didSelectRowAtIndexPath方法中,

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

这可以帮助我完美地首次加载或弹出导航。

于 2015-07-19T12:11:05.077 回答
2

Swift 4 和 5:最初只选择一次单元格而不willDisplayCell重置其状态:

override func viewWillAppear(_ animated: Bool) {
    for section in 0..<tableView.numberOfSections {
        for row in 0..<tableView.numberOfRows(inSection: section) {
            let indexPath = IndexPath(row: row, section: section)

            if /* your condition for selecting here */ {
                _ = tableView.delegate?.tableView?(tableView, willSelectRowAt: indexPath) // remove if you don't want to inform the delegate
                tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
                tableView.delegate?.tableView?(tableView, didSelectRowAt: indexPath) // remove if you don't want to inform the delegate
            }
        }
    }
}
于 2019-03-12T22:54:15.747 回答
1

Swift 5,非常感谢@Ravindhiran 的回答:

let selectedCellIndexPath = IndexPath(row: 0, section: 0)
tableView(tableViewList, didSelectRowAt: selectedCellIndexPath)
tableViewList.selectRow(at: selectedCellIndexPath, animated: false, scrollPosition: .none)

或者

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if /* should be selected */{
          cell.setSelected(true, animated: false)
     }
}
于 2019-04-30T08:24:42.113 回答
1

斯威夫特 5.2

override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: true)
        accessoryType = selected ? .checkmark : .none
        backgroundColor = selected ? UIColor.secondarySystemBackground : UIColor.systemBackground
        myLabel.textColor = selected ? .tertiaryLabel : .secondaryLabel
        self.isUserInteractionEnabled = selected ? false : true

    }

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

        let cell = cell as! CustomTableViewCell

        if (/* condition is met */) {
            cell.setSelected(true, animated: true)

        }
    }

于 2020-05-19T17:39:09.570 回答
0
 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // reset cell state
        cell.accessoryType = .none  // (if you needed)
        tableView.deselectRow(at: indexPath, animated: false)

        // if need select
        cell.accessoryType = .checkmark // (if you needed)
        tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
    }

// method work fine when tap cell 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

使用 [cell setSelected:YES Animation:NO]; 单元格点击 didSelectRowAt,didDeselectRowAt 不起作用

于 2019-07-02T09:56:35.780 回答
0

我为这个初始选择奋斗了 5 个多小时。使用tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)对我有用!

谢谢大家。

于 2021-04-21T20:51:18.370 回答
0

我将单元格中的所有选定项目添加到 selectedArray 中。在willDisplay中,我正在寻找 selectedArray 中的当前元素。如果我找到它

 tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)

在我的代码中,它看起来像:

   func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let contact = sortedArray[indexPath.section].sectionObjects[indexPath.row]        
    for selectedContact in selectedContacts{
        if selectedContact == contact{
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
        }
    }
}

在这种情况下,预选单元格和新选择的单元格都可以很好地工作,滚动表格时也没有问题didSelectRowAtdidDeselectRowAt

于 2021-10-10T21:58:53.167 回答