我以编程方式实现了自定义UITableViewCell,其中在cellforindexpath中创建了UIButton和UILabel。
对于UIButton,我使用了addTarget:action:forControlEvents:
方法来调用另一个方法,该方法发出HTTP发布请求,以更新服务器中的UILabel文本。但是,现在我想更新UILabel中的文本,而不是重新加载应用程序。
我该怎么做呢?
我的问题是UILabel和UIButton都是UITableViewCell的子视图,因此它们之间没有直接通信。
最佳答案
我认为,如果您创建标签和按钮是您创建的自定义单元格的字段。然后将动作处理程序也放在自定义单元格上,您将可以轻松访问标签。
就像这样:
CustomCell.h
UIButton *button;
UILabel *label;
-(IBAction) updateLabel:(id) sender;
CustomCell.m
更新资料
//[button addAction:@selector(updateLabel) target:self];
-(IBAction) updateLabel:(id) sender{
[self.label setText:@" text"];
}
您可以使用Interface Builder实施自定义单元。您也可以从Interface Builder将操作连接到按钮,还需要将操作原型移至头文件。使文件所有者成为ViewTableController来加载该单元格。
然后在TableViewController上为自定义单元创建一个出口,然后在委托方法上像这样加载自定义单元:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)table_view cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cell_identifier = @"Cell";
CustomCell *cell;
cell=(CustomCell*)[table_view dequeueReusableCellWithIdentifier:cell_identifier];
if (cell==nil) {
self.customCell=nil;
[[NSBundle mainBundle] loadNibNamed:@"custom_cell" owner:self options:nil];
cell=self.customCell;
}
}
关于objective-c - iOS:UITableViewCell中的UIButton和UILabel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6018691/