iOS- uitaiteViewCell的动态onclick函数



我没有问题在UITableview中使用didSelectRowAtIndexPath创建on click事件,问题是我宁愿在设置单元格的同时设置on click事件,因为并非每个单元格需要一个on click事件,根据它是哪个单元,它们的工作方式会有所不同。有没有办法设置单击以专门的详细文本?也许创建一个与文本完美匹配的覆盖框架?

如果这是不可行的,我会尽力而为,但这将是很多代码。

如果我需要阐述自己的问题,请让我知道我是否根本令人困惑。谢谢。

您可以将事件添加到您想要的单元格中的按钮中,并像标签一样格式化它。

[button addTarget:self action:@selector(onClickDescription:) forControlEvents:UIControlEventTouchUpInside];

,然后在代码中:

-(void)onClickDescription { 
     CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.yourTableView];
     NSIndexPath *indexPath = [self.yourTableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
        //Do what you need
    }
}

自定义:

  1. 在故事板中使用"自定义单元"添加标签和一个按钮和标签1和2。

  2. 在ViewForcellatIndExpath中只是放置:

    UILabel *label = (UILabel *)[cell viewWithTag:1]; UIButton *button = (UIButton *)[cell viewWithTag:2];

  3. 现在您可以格式化,但是您想格式化

    label.font = .... label.textColor = ....

最新更新