如何在UITableView上不使用自定义单元格的情况下添加按钮?

Rah*_*yas 2 iphone cocoa-touch uitableview

如何在a上添加自定义按钮UITableViewCell,然后使用该按钮删除单元格而不使用Interface Builder和Custom Cell?

Cor*_*oyd 8

如果你真的想添加一个没有子类的自定义按钮,只需将按钮添加到单元格的contentView:

[cell.contentView addSubview:customButton];
Run Code Online (Sandbox Code Playgroud)

您可以设置按钮的所有特征:框架,目标,选择器等...然后广告使用上述调用将其添加到单元格.

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=//whatever
[customButton setImage:anImage forState:UIControlStateNormal];
[customButton setImage:anotherImage forState:UIControlStateHighlighted];
[customButton addTarget:self action:@selector(delete) forControlEvents: UIControlEventTouchUpInside];
//yadda, yadda, .....
Run Code Online (Sandbox Code Playgroud)

你也可以标记它

customButton.tag = 99999;
Run Code Online (Sandbox Code Playgroud)

所以你以后可以找到它:

UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999];
Run Code Online (Sandbox Code Playgroud)

您需要决定何时添加按钮,可能在单元格选择上,也许在编辑模式下...只需将代码放入您选择的委托方法中.