2011-05-16 39 views
1

我以编程方式实现了一个自定义的UITableViewCell,其中在cellforindexpath中,我创建了一个UIButton和UILabel。iOS:在UITableViewCell中的UIButton和UILabel

对于UIButton,我使用addTarget:action:forControlEvents:方法来调用另一个方法,它发出HTTP POST请求,更新服务器中的UILabel文本。但是,现在我想更新UILabel中的文本,而不是重新加载应用程序。

我该怎么做?

我遇到的问题是UILabel和UIButton都是UITableViewCell的子视图,所以它们之间没有直接的通信。

回答

1

我认为如果您制作标签和按钮是您制作的自定义单元格的字段。然后在自定义单元格上创建动作处理程序,您将可以轻松访问该标签。 这将是类似的东西:

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的按钮,您需要将动作原型移动到头文件。使文件所有者成为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; 
    } 
} 
+0

我编程的UIButton和编程的UILabel所以没有CustomCell类...有另一种办法? – SuperString 2011-05-16 15:47:10

+0

我试过这样做,但很难,因为CustomCell是UITableView的子视图,所以它很复杂。 – SuperString 2011-05-16 15:47:57

+0

莎拉 - 我只是将标签制作成自定义单元的一种属性,与苹果当前的风格保持一致。此外,您可以使用UINib直接加载单元而不需要占位符所有者对象。 – nielsbot 2011-05-16 18:15:15