【问题标题】:Make cell not clickable, but a button on it should still be clickable使单元格不可点击,但其上的按钮仍应可点击
【发布时间】:2013-08-12 14:38:46
【问题描述】:

我有一个带有一些单元格的 tableView。每个单元格还包含一个按钮。当用户点击按钮时,单元格应该是不可点击的,但不是按钮。因此,当用户单击不可点击的单元格的按钮时,该单元格应该可以再次点击。

我试过了:

cell.userInteractionEnabled = NO;

...但是按钮不再可点击。

提前感谢您的努力。

编辑 我的意思是:当我单击一个单元格时,会打开一个新视图。但我希望,当单元格不可“点击”时,不会发生任何操作。

【问题讨论】:

  • 尝试在各个控件上设置userInteractionEnabled
  • @MarcusAdams 但是我该怎么做呢?

标签: ios button uitableview clickable


【解决方案1】:

以哪种方式无法点击?如果您只是希望单元格不可选,您可能正在寻找这个:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

如果您想阻止在禁用选择时执行代码,只需检查 didSelectRowAtIndexPath:method 中的 selection 属性即可。像这样的:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell.selectionStyle != UITableViewCellSelectionStyleNone) {
        //(your code opening a new view)
    }
}

请记住,您仍然需要使用此属性,当您不希望单元格可选择时设置为UITableViewCellSelectionStyleNone,并在您希望它可选择时设置回UITableViewCellSelectionStyleBlue(或UITableViewCellSelectionStyleGray)可以再次选择。

【讨论】:

  • 但是我仍然可以单击单元格并打开新的 viewController...我认识到的唯一区别是当单元格被选中时它们没有颜色...或者我这里有什么问题
  • 已编辑。试试看是否有帮助。
  • @LucasEduardo 好的……我明白了。当我使用故事板时,是否也可以将其与prepareForSegue 方法结合使用?
  • 这取决于你想要做什么。问题是因为在prepareForSegue 中您没有对所选索引路径(以及因此单元格)的引用,因此您必须将所选单元格存储在某个实例变量或类似的东西中。如果 anwser 帮助了您,请不要忘记接受它,以便将您的问题从“未回答”部分中删除!
  • 对于 swift 2,语法略有不同:cell.selectionStyle = .None;
【解决方案2】:

Swift 版本:

cell.selectionStyle = .none

【讨论】:

    【解决方案3】:

    通过将UITableViewCellSelectionStyleNone 设置为selectionStyle 来删除选择。

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    -tableView:didSelectRowAtIndexPath:什么都不做

    您可以在该委托方法中进行选择,例如,如果只有第一部分中的第一行有按钮并且不应该做任何事情:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSIndexPath *indexPathForDisabledCell = [NSIndexPath indexPathForRow:0
                                                                   inSection:0];
        if([indexPath compare:indexPathForDisabledCell] != NSOrderedSame) {
            //Do whatever you do with other cells
        }
    }
    

    【讨论】:

      【解决方案4】:

      这也可以通过 Interface Builder 使用 TableViewCell 上的用户定义的运行时属性来完成:

      Key Path | Type | Value

      selectionStyle | Number | 0

      https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/c/tdef/UITableViewCellStyle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-14
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-15
        • 2020-11-24
        • 2017-04-16
        相关资源
        最近更新 更多