ios 初始选择的 UITableViewCell 集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19295297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:15:42  来源:igfitidea点击:

UITableViewCell Set selected initially

iosuitableviewuisplitviewcontroller

提问by Sagar In

Hi there I have situation where in an iPad app my master controllerhas list and details controllerhave its details , a typical UISplitViewControllerpattern. 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 setSelectedand in didSelectRowAtIndexPathmethod removing selection like this.

嗨,我有这样的情况,在 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];
} 

for initial cell but my none cell is getting selected after that please help me out.

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

回答by Drew C

For the cell to appear selected, you have to call -setSelected:animated:from within -tableView:willDisplayCell:forRowAtIndexPath:like so:

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

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

Calling -setSelectedfrom anywhere else has no effect.

-setSelected从其他任何地方调用都没有效果。

回答by Ravindhiran

try this

尝试这个

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

Swift 4 version is

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

回答by Peter Kreinz

Swift 4:Selecting cell initially

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

    ...
}

回答by wesley

I don't know you are expecting this answer. By default if you wants to select first row in your tableView with out manual selection, just initiate the didSelectRowAtIndexPathmethod like this

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

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

回答by Ashu

It will also help in the case of POP Navigation

它也将有助于 POP 导航

.h file,

.h 文件,

NSIndexPath *defaultSelectedCell

.m File

.m 文件

Put this code in ViewDidLoad

把这段代码放进去 ViewDidLoad

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

in viewDidAppear

viewDidAppear

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

This will help when you POP, Put this code in viewWillAppear

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

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

in tableView didSelectRowAtIndexPathMethod,

tableView didSelectRowAtIndexPath方法中,

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

This helps me perfectly either first time loading or Pop Navigation.

无论是第一次加载还是弹出导航,这都可以完美地帮助我。

回答by Lal Krishna

Best Option

最佳选择

Swift 5

斯威夫特 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)
    }
}

回答by Jonathan Cabrera

Swift 4 and 5:Selecting cells initially just one time without willDisplayCellresetting their status:

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

回答by dengST30

Swift 5, Thanks @Ravindhiran's answer a lot:

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

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

or

或者

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if /* should be selected */{
          cell.setSelected(true, animated: false)
     }
}

回答by yuelong qin

 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)

use [cell setSelected:YES animated:NO]; cell tap didSelectRowAt, didDeselectRowAt not work

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

回答by Tech UK

Swift 5.2

斯威夫特 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)

        }
    }