2011-05-16 51 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