2013-01-05 30 views
2

我所擁有的是在單獨的.xib中創建的自定義表格單元格。並且爲此我有一個Objective-C類。我將自定義單元格中的標籤連接到了自定義單元格的類。從主ViewController類中更改單獨的.xib中的標籤文本

在我的主要.xib中,我有一個TableView自定義單元格被添加到。填充的代碼位於主類(ViewController.m)中。

那麼如何從主類(ViewController.m)更改定製單元格中的label

當用戶點擊定製單元,示出了對話框,並在自定義單元格的label的文本是根據在對話框中選擇的按鈕改變。

回答

0

我想通了不同的方法,目前我已經不再iOS開發,因爲我討厭它,哈哈

不管怎麼說,謝謝你們這麼快回復。

1

您必須爲標籤指定一個標籤,即「接口」構建器中的99。 然後,在ViewController.m,當你野兔裝入電池,裝載廈門國際銀行後,你可以做

UILabel *label = [cell viewWithTag: 99]; 
label.text = @"Some text here... (:"; 

這將這樣的伎倆! :)

2

由於它是一個表格單元格,我假設你在表格視圖中使用它。您通常通過

- (UITableViewCell *)UITableView:(UITableView *) cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"myCustomCell"; 
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil]; 
     for (id anObject in nib) { 
      if ([anObject isKindOfClass:[MyCustomCell class]]) { 
       cell = (MyCustomCell *)anObject; 
      } 
     } 
    } 
    cell.myLabel.text = @"Some Text"; // This will set myLabel text to "Some Text" 
    return cell; 
} 
1

它做到這一點很簡單:

在customcell首先創建標籤的屬性

Customcell.h

@interface CustomCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UILabel *yourLabel; 

現在創建customcell實例您的TableViewController

YourTableViewController.h 
@interface YourTableViewController : UITableViewController< 

    { 
     CustomCell *cell; 
    } 

和YourTableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:nil]; 

    if (cell == nil) 
    { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (CustomCell *) currentObject; 
       break; 

      } 
     } 
    } 

cell.yourLabel.text = @"whatever you want to add"; 
現在

,如果你想在其他的方法來更新customcell的標籤不僅僅是做到這一點。

-(void)someMethod() 
{ 
CustomCell *acell = (CustomCell *)[tableView cellForRowAtIndexPath:n]; 
acell.yourLabel.text = @"whatever you want to add."; 
} 
相關問題