userInteractionEnabled禁用单元格中的所有元素

时间:2013-10-24 08:46:40

标签: ios objective-c uitableview user-interaction

我想禁用细胞互动。在我的功能中:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

我添加了

Cell.userInteractionEnabled = NO;

禁用了单元格交互,但我想保留活动按钮(ButtonLabel)。

Cell.userInteractionEnabled = NO;
Cell.ButtonLabel.userInteractionEnabled = YES;

上述代码不起作用。这很奇怪,因为逆转是可以的:

Cell.userInteractionEnabled = YES;
Cell.ButtonLabel.userInteractionEnabled = NO;

按钮已禁用,但单元格编号

如何在禁用单元格时激活按钮?

3 个答案:

答案 0 :(得分:3)

在UIView(或其子类)的任何实例上设置userInteractionEnabledNO时,它会自动禁用所有子视图上的用户交互。这正是您所描述的行为。

我认为您要做的是禁用UITableViewCell上的选择。有几种方法可以做到:

  1. 如果您想完全阻止所有单元格的选择,您可以 只需将tableView.allowsSelection设置为NO
  2. 如果您想仅阻止在某些单元格上进行选择,请执行Cell.selectionStyle = UITableViewCellSelectionStyleNone;
  3. 您还可以覆盖- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;并返回nil以查找不应触发选择操作的单元格(但仅此一项不会阻止选择突出显示出现。)

答案 1 :(得分:1)

如果禁用单元格上的交互,则会影响所有单元格元素。您想要做的是将单元格设置为UITableViewCellSelectionStyleNone

这将有效

 [Cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Cell.ButtonLabel.userInteractionEnabled = YES;

答案 2 :(得分:1)

根据您的评论,您需要禁用特定单元格的推送连接。您可以做的是检查shoudlPerformSegueWithIdentifier,如果该单元格应该执行。

您需要保存触摸的单元格,然后:

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if (/*check if the selected cell should perform the segue*/) {
        return YES;
    }

    return NO;
}
相关问题