我以编程方式实现了一个自定义 UITableViewCell,在 cellforindexpath 中,我创建了一个 UIButton 和 UILabel。
对于 UIButton,我使用 addTarget:action:forControlEvents:
方法调用另一个方法,该方法发出 HTTP post 请求,更新服务器中的 UILabel 文本。但是,现在我想更新 UILabel 中的文本,而不是重新加载应用。
我该怎么做?
我遇到的问题是 UILabel 和 UIButton 都是 UITableViewCell 的 subview ,因此它们之间没有直接通信。
最佳答案
我认为如果您制作的标签和按钮是您制作的自定义单元格的字段。然后在自定义单元格上也制作 Action 处理程序,您将能够轻松访问标签。 应该是这样的:
自定义单元格.h
UIButton *button;
UILabel *label;
-(IBAction) updateLabel:(id) sender;
自定义单元格.m
更新
//[button addAction:@selector(updateLabel) target:self];
-(IBAction) updateLabel:(id) sender{
[self.label setText:@" text"];
}
您可以使用 Interface Builder 实现自定义单元格。您也可以将操作连接到 Interface Builder 中的按钮,您需要将操作原型(prototype)移动到头文件中。使文件所有者成为 ViewTableController 以加载该单元格。 然后在 TableViewController 和委托(delegate)方法上为您的自定义单元格创建一个导出,您可以像这样加载自定义单元格:
// 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/