objective-c - iOS:UITableViewCell 中的 UIButton 和 UILabel

标签 objective-c cocoa-touch ios

我以编程方式实现了一个自定义 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/

相关文章:

ios - 使用 drawInRect 调整图像大小,同时保持像 Scale Aspect Fill 这样的纵横比?

ios - 将 block 并发插入并发队列

objective-c - 子类化单例

iphone - 存储多个关联在一起的数组的最佳方式 - Objective C

iphone - 平滑的变形动画

ios - 无法在 Google 开发者控制台上创建新项目

ios - 如何存档文本颜色为渐变的 UItextfield?

objective-c - 如何在运行时识别协议(protocol)方法是可选的?

ios - 出现键盘后UIWebView自动上移

objective-c - URL applicationWillTerminate Objective-C