ios - 单击 UITableViewCell 中的 UIbuttion 如何更改 uilabel 文本

标签 ios cocoa-touch uitableview uikit

我在这个自定义单元格下方显示了 UITableViewCell 单元格,它有两个用于添加/减去产品数量的按钮。我已将当前数量设置为 + 按钮标签并且能够获取。但是我将如何设置标签文本 2 等等当用户点击 + 按钮时。

I have UITableViewCell

我不想更改 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/

相关文章:

ios - iPhone 上的负值数据使用情况?

ios - Iphone 模拟器屏幕键盘问题

iphone - [AVPlaybackItem fpItem] : message sent to deallocated instance

iphone - 特定角的角半径

iphone - 在-tableView :heightForRowAtIndexPath: when there is a section index present期间获取tableview单元格的真实宽度

ios - SwiftUI:如何在另一个 View 中更新传递的数组项

ios - 从 Parse 中检索图像

ios - 使用Auto Layout时相当于 "CGAffineTransformMakeTranslation (0,0)"?动画回到原来的位置

ios - 无法获取 tableView 中的单元格数量

ios - 当我滚动时,TableView 仅加载一个单元格的内容