如何添加一个按钮而不使用UITableView上的自定义单元格? [英] how to add a button without using custom cell on a UITableView?

查看:94
本文介绍了如何添加一个按钮而不使用UITableView上的自定义单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I add a custom button on a UITableViewCell, and then delete the cell with that button without using Interface Builder and Custom Cell?

推荐答案

如果你真的想添加一个自定义按钮而不需要子类化,只需将该按钮添加到单元格的contentView:

If you really want to add a custom button WITHOUT subclassing, just add the button to the contentView of the cell:

[cell.contentView addSubview:customButton];

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

You can set all the characteristics of the button: frame, target, selector, etc... Ad then used the above call to add it to the cell.

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, .....

customButton.tag = 99999;

因此您可以稍后找到:

UIButton *abutton = (UIButton*) [cell.contentView viewWithTag:99999];

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

You will need to decide WHEN to add the button, maybe on cell selection, maybe in editing mode... just put the code in the delegate method of your choice.

这篇关于如何添加一个按钮而不使用UITableView上的自定义单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆