2012-09-14 45 views
3

我做了UITableViewCell的子類以獲得更大的TableViewCell以及更多選項。UITableViewCell子類不允許我設置標籤文本

但我的問題是,我不能設置標籤(一個或多個)的文本(S):

BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]]; 

NSLog(@"%@", [bi title]); 
[[cell mainLabel] setText:[bi title]]; 
NSLog(@"%@", [[cell mainLabel] text]); 

第一個日誌消息返回我的預期的內容,但第二個會隨時記錄(空值)。

我真的不知道什麼應該是錯的。我已經像往常一樣創建標籤:

@property (weak, nonatomic) IBOutlet UILabel *mainLabel; 

當然,我連接標籤併合成它們(檢查兩次)。我也實施了方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

以獲得適當的高度爲每個單元格(工作正常)。

順便說一句,檢查標記按預期顯示。這只是關於標籤。

+1

外貌對我來說** ** cell **是**零**。嘗試首先記錄,以確保您確實擁有一個單元格對象。 –

+0

你有沒有附帶單元格的標籤Outlet ...在xib文件中?如果是,那麼檢查你的單元格,要麼是零? –

+0

單元格存在: 例如: > –

回答

1

確保您在cellForRowAtIndexPath方法中實例化了您自定義的UITableViewCell子類。還要確保那些IBOutlets在您的UITableView子類中聲明,而不在包含TableView的View Controller中聲明。還要確保界面生成器中的單元的父類被設置爲相同的子類。

像這樣的東西(自定義UITableViewCell子接口文件):

#import <UIKit/UIKit.h> 

@interface MyCustomCell : UITableViewCell 
    @property (nonatomic, weak) IBOutlet UILabel *mainLabel; 
@end 

然後@synthesize在實現文件:

@synthesize mainLabel; 
cellForRowAtIndexPath

然後,像:

static NSString *CellIdentifier = @"MyCellIdentifier"; 
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]]; 

// Configure the cell... 
cell.mainLabel.text = [bi title]; 
// ... Other stuff 
return cell; 
+0

好吧,我的代碼看起來像這樣。唯一的區別是你宣佈IBOutlet是強大的。當我學習編程時,我被告知總是將IBOutlets設置爲弱性。 我已經將它改爲強壯並初始化它們。現在文本至少設置正確。因此,它不顯示任何東西。 –

+0

現在就開始編程。這樣可行。 –