4.6 Creating Custom Table View Cell Accessories

本文介绍如何在UITableViewCell中自定义UI元素,如使用UIButton作为accessoryView,并实现点击事件处理。通过UIButton的superview可以找到对应的UITableViewCell。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义TableViewCell
有时SDK提供的那几个简单的label远远不能满足我们的需要,我们需要更丰富的界面,这时我们可以利用accessoryView属性。你可以给他赋值我们需要的UIView。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* result = nil;
......
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0f, 0.0f, 150.0f, 25.0f);
[button setTitle:@"Expand" forState:UIControlStateNormal];
[button addTarget:self action:@selector(performExpand:)
 forControlEvents:UIControlEventTouchUpInside];
result.accessoryView = button;//在这里赋值您需要的UIView
return result;
}
- (void) performExpand:(id)paramSender{
/* Take an action here */
}

这是在列表中的每一行都会显示一个button。点击button会触发 performExpand 方法。
传过来的paramSender就是button对象,可是如果我想知道是哪一行的button被点击了,及获取button所在的TableViewCell,改怎么获取呢?
TableViewCell 是把 accessoryView 当做subview 的,所以我们可以通过button的superview来获取。
- (void) performExpand:(UIButton *)paramSender{
UITableViewCell *ownerCell = (UITableViewCell*)paramSender.superview;
if (ownerCell != nil){
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值