cell中button怎么得到对应cell的indexpath 以及关于UITableViewCellContentView的问题


============================================================
博文原创,转载请声明出处
============================================================
我们用这种方法去创建cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
dequeueReusableCellWithIdentifier:cellIdentifier];
    UITableViewCell* cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
return cell;
}

那么,相应我们button的selector中,我们可以通过下面的方法或侧indexpath


-(void)buttonaction:(UIButton*)sender{
    UIView* v=[sender superview];//UITableViewCellContentView
    UITableViewCell* cell=(UITableViewCell*)[v superview];//UITableViewCell
    NSIndexPath* indexPath= [_tbSelect indexPathForCell:cell];
}

这里我们用了两个个superview,第一个得到UITableViewCellContentView,第二个得到相应的UITableViewCell。

同样。我们使用xib,拖进去一个UITableViewCell也是可以的


问题来了,


----------上面的方法我们成为方法A,下面的方法我们成为方法B

一次,我拖进去一个UIView,然后将class改为UITableViewCell,使用上面的方法不行。

而是只能使用一个superview

 UITableViewCell* cell=(UITableViewCell*)[sender superview];//UITableViewCell

后来研究发现方法A中,使用的是系统的UITableViewCell,因此自动添加了UITableViewCellContentView

而在方法B中,我们是自己修改的,因此没有UITableViewCellContentView

打印方法:

NSLog(@"----start");
    for (UIView* v in [cell subviews]) {
        NSLog(@"v:%@",v);
    }

打印结果:

//方法A
v:<UITableViewCellContentView: 0xa517a20; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0xa518f90>; layer = <CALayer: 0xa51e450>>

//方法B
---start
2013-07-16 14:04:12.529 demo[2572:c07] v:<UILabel: 0x10f80a60; frame = (30 11; 215 21); text = 'Label'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; tag = 101; layer = <CALayer: 0x10f80af0>>
2013-07-16 14:04:12.530 demo[2572:c07] v:<UICheckBox: 0x10f7f680; baseClass = UIButton; frame = (260 7; 30 30); opaque = NO; autoresize = RM+BM; tag = 102; layer = <CALayer: 0x10f7f740>>


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在table view的某一列实现button,可以通过以下步骤实现: 1. 在table view的数据源添加一个按钮的属性,用来记录当前cell需要展示的按钮状态。 2. 在table view的代理方法,处理cell的按钮状态。可以通过为cell添加一个UIButton的子视图,并设置按钮的标题、样式等来实现。 3. 在点击按钮时,更新数据源的按钮状态,并重新加载对应cell。 以下是一个简单的示例代码: ``` // 数据源添加按钮的状态属性 enum ButtonState { case normal case selected } struct Item { var title: String var buttonState: ButtonState } var items: [Item] = [ Item(title: "item 1", buttonState: .normal), Item(title: "item 2", buttonState: .normal), Item(title: "item 3", buttonState: .normal), ] // 处理cell的按钮状态 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) let item = items[indexPath.row] cell.textLabel?.text = item.title // 添加按钮 let button = UIButton(type: .system) button.frame = CGRect(x: cell.contentView.bounds.width - 80, y: 0, width: 80, height: cell.contentView.bounds.height) button.titleLabel?.font = UIFont.systemFont(ofSize: 16) button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside) cell.contentView.addSubview(button) // 设置按钮的标题和样式 switch item.buttonState { case .normal: button.setTitle("Normal", for: .normal) button.setTitleColor(.blue, for: .normal) case .selected: button.setTitle("Selected", for: .normal) button.setTitleColor(.red, for: .normal) } return cell } // 点击按钮时更新按钮的状态 @objc func buttonTapped(_ sender: UIButton) { guard let cell = sender.superview?.superview as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) else { return } switch items[indexPath.row].buttonState { case .normal: items[indexPath.row].buttonState = .selected case .selected: items[indexPath.row].buttonState = .normal } tableView.reloadData() } ``` 在以上示例代码,我们通过ButtonState枚举来记录按钮的状态。在处理cell的按钮状态时,我们添加了一个UIButton的子视图,并根据按钮的状态设置按钮的标题和样式。在点击按钮时,我们根据按钮所在的cellindexPath更新按钮的状态,并重新加载对应cell。需要注意的是,由于UIButton添加到cell.contentView上,因此需要调用sender.superview?.superview来获取cell的引用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值