UITableViewCell Set最初选择

时间:2013-10-10 12:05:15

标签: ios uitableview uisplitviewcontroller

您好我的情况是,在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];
} 

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

10 个答案:

答案 0 :(得分:78)

要让单元格显示为已选中,您必须在-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无效。

答案 1 :(得分:50)

试试这个

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

答案 2 :(得分:35)

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

    ...
}

答案 3 :(得分:10)

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

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

答案 4 :(得分: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];

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

答案 5 :(得分:1)

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

        }
    }

答案 6 :(得分:0)

最佳选择

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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

...
$(".window-one").mouseover(function() {$(".window-one-modal").removeClass("def-visible");});
$(".window-one").mouseout(function() {$(".window-one-modal").addClass("def-visible");});

$(".window-two").mouseover(function() {$(".window-two-modal").removeClass("def-visible");});
$(".window-two").mouseout(function() {$(".window-two-modal").addClass("def-visible");});

$(".window-three").mouseover(function() {$(".window-three-modal").removeClass("def-visible");});
$(".window-three").mouseout(function() {$(".window-three-modal").addClass("def-visible");});
...
  

使用[单元格setSelected:是动画:否];单元点击didSelectRowAt,didDeselectRowAt不起作用

答案 9 :(得分:0)

我为这个最初的选择苦苦挣扎了 5 个多小时。使用 tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) 对我有用!

谢谢大家。

相关问题