2013-07-08 40 views
4

自定义按钮如何禁用UserInteractionUITableview细胞,但没有在该单元格的自定义按钮..如何禁用用户交互的UITableView的细胞,但不是在细胞

我创造的一个按钮:

UIButton *dayButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [dayButton setFrame:CGRectMake(201, 0, 30, 35)]; 
    [dayButton addTarget:self action:@selector(selectDayView) forControlEvents:UIControlEventTouchUpInside]; 
    [dayButton setBackgroundImage:[UIImage imageNamed:@"86-camera.png"] forState:UIControlStateNormal]; 
    dayButton.userInteractionEnabled=YES; 
    [cell addSubview:dayButton]; 

然后我设置

cell.userInteractionEnabled=NO; 

我怎样才能得到dayButtonAction

回答

0

不可能为UITableView的,而不是UITableViewCell适用禁止userInteraction。当启用UITabelView时,您可以访问UITableVie的内容/控制器(子视图UITableView)。当您在UITableView添加任何控制器实际上你加它UITableViewCell,但电池是UITableView一部分,所以当你在UITableView禁用userInteraction这意味着它ASLO设置(NO)的UITableViewCell的。

这里我ASLO给你建议你UIButton添加到细胞作为

[cell.contentView addSubView:buttonName]; 
3

如果您设置了cell.userinteraction = NO,那么您将无法触摸按钮,因为cell及其subviews的任何交互均已关闭。

9

不要禁用用户与整个单元格的交互。设置cell.selectionStyle = UITableViewCellSelectionStyleNone以防止单元格选择。这样用户仍然可以与按钮进行交互。

1

您可以设置[cell setSelectionStyle:UITableViewCellSelectionStyleNone];也没有实现tblView:didSelectRowAtIndexPath:。让你点击按钮(因为它会默认)。处理它的事件来执行你的任务。

由于我明白如果上海华启用了userintercation = no比它的子视图不能互动。在你的情况下,UITableViewCell是超级视图,按钮是子视图。所以最好避免设置cell.userInteractionEnabled = NO;并使用UITableViewCellSelectionStyleNone

希望这有助于:)

1

如果cell.userinteraction = NO则无法触摸它的subviews。简单的方法是只要UIViewclearColour和视图的大小是相同的你的单元格大小,并把它放在单元格和俯视图上,你可以设置你的UIButton。当您想要cell.userinteraction = NO时,只需设置view.hidden=NO,然后cell.userinteraction = Yes然后设置view.hidden=YES。所以你的透明UIView显示和以上查看您的UIButton然后你可以得到点击按钮。无需设置cell.userinteraction

0

您无法在单元上设置用户交互禁用,因为它是添加了按钮的超级视图。如果您这样做,您将无法与按钮交互,因此禁用UITableViewCell的其余子视图的交互。

2

一个古老的问题,但认为这可能有助于某人。您可以通过将选择属性设置为不选,禁用UITableView的用户交互。

Identity Inspector

1

还有一个选项是重写的touchesBegan和touchesEnded。具体方法如下:

1)使具有压倒一切的功能和指标自定义单元格应用它们或不

class MyCustomTableViewCell: UITableViewCell { 
    var shouldDisableTouches: Bool = false 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if !shouldDisableTouches { super.touchesBegan(touches, with: event) } 
    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if !shouldDisableTouches { super.touchesEnded(touches, with: event) } 
    } 
} 

2)故事板设置你的类这个类中定义类>类

3)现在的tableView(_:cellForRowAt :)你可以打开或关闭

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier", for: indexPath) as! MyCustomTableViewCell 
    if <your condition here> { 
     cell.shouldDisableTouches = true 
    } else { 
     cell.shouldDisableTouches = false 
    } 
} 

互动......在“其他”部分是必不可少这里,不要放弃它。毕竟它是一个可重用的单元。

这种禁用不会影响位于单独的xib中的按钮,该按钮用于注册此单元格。还没有在单元上测试过它,直接在故事板中设计。