我在这个自定义单元格下方显示了 UITableViewCell 单元格,它有两个用于添加/减去产品数量的按钮。我已将当前数量设置为 + 按钮标签并且能够获取。但是我将如何设置标签文本 2 等等当用户点击 + 按钮时。
我不想更改 TableView 的数据源,即在数据进入 UITableView 的数组中添加增量值,然后重新加载 tableView。
同样在探索 SO 时我发现
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tblCart];
NSIndexPath *hitIndex = [self.tblCart indexPathForRowAtPoint:hitPoint];
获取具有按钮的单元格的 NSIndexPath。
最佳答案
您应该在您的CustomCell.h
文件中创建delegate
方法
@class CustomCell;
@protocol CellDelegate <NSObject>
-(void)plusButtonClick:(CustomCell *)cell;
@end
.h文件的内部接口(interface)
@interface CustomCell : UITableViewCell
@property id<CellDelegate> delegate;
@end
然后在 plusButton 的 Click 事件上从 CustonmCell.m
调用 delegate
,例如
if ([delegate respondsToSelector:@selector(plusButtonClick:)]) {
[delegate plusButtonClick:self];
}
现在在你的 viewController
-(void)plusButtonClick:(CustomCell *)cell{
int count = [[cell.textLabel text]intValue];
count++;
cell.textLabel.text = [NSString stringWithFormat:@"%d",count];
}
希望这就是你想要的。
关于ios - 单击 UITableViewCell 中的 UIbuttion 如何更改 uilabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26503572/