我有一个标准方法,它在表格中形成单元格:
- (SearchTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.button.tag = 3;
}
细胞内有按钮,有动作:
- (IBAction)changeName:(id)sender {
// GOTO
}
点击后如何更改按钮的名称
答案 0 :(得分:0)
试一试:
- (IBAction)changeName:(id)sender {
UIButton *button = (UIButton *)sender;
if (button.tag == 3) {
[button setTitle:@"Your title" forState:UIControlStateNormal];
}
}
请勿忘记将您的方法与笔尖中的按钮相关联。
答案 1 :(得分:-1)
使用协议委托方法
自定义单元格.h文件的代码
#import <UIKit/UIKit.h>
@protocol CustomCellProtocal <NSObject>
-(void)CellButtonClicked:(UIButton *)sender;
@end
@interface CustomCell : UITableViewCell
@property(weak) id<CustomCellProtocal>delegate;
- (IBAction)BtnClicked:(id)sender;
@end
自定义单元格的代码.m文件
#import "CustomCell.h"
@implementation CustomCell
@synthesize delegate;
- (IBAction)BtnClicked:(id)sender {
[delegate CellButtonClicked:sender];
}
@end
另一种最简单的方式
我建议你创建具有Button的自定义单元格。在自定义单元格类中为该按钮创建插座。
然后在cellForRowAtIndexpath()中将目标添加到该按钮,如下所示
[CustomCell.buttonOutlet addTarget:self action:@selector(BtnClicked:) forControlEvents:UIControlEventTouchUpInside];
然后用BtnClicked方法处理你的逻辑。
-(void)BtnClicked:(UIButton *)sender{
if(sender.tag == 1){
[sender setTitle:@"Hello" forState:UIControlStateNormal];
}
}
我强烈建议,第二种方式最适合您的情况。细胞将被标识符所取代。所以不用担心。