2012-08-03 134 views
0

我搜索得太多了,嘗試了很多想法,但我無法使它工作。自定義UITableCell不能正常工作

我有一個視圖控制器,並在該控制器我有一個tableview有自定義單元格。 我連接插座,數據源和委託,但是當我運行該項目,而不是我的自定義單元格。顯示UITableCell並且不顯示數據。這裏是我的.h和.m文件

#import <UIKit/UIKit.h> 
@class SelectMutantCell; 
@interface MutantViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>{ 
UITableView *tableView; 
IBOutlet SelectMutantCell *mutantCell; 
} 

@property (nonatomic, weak) IBOutlet UITableView *tableView; 
@property (nonatomic, weak) SelectMutantCell *mutantCell;  

- (IBAction)cancel:(id)sender; 

@end 

,這裏是我的.m文件

@implementation MutantViewController 

@synthesize tableView = _tableView; 
@synthesize mutantCell = _mutantCell; 

NSArray *mutants; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //mutants = [mutants initWithObjects:@"Keko",@"HEHE",@"PEPE", nil]; 
    UINib *cellNib = [UINib nibWithNibName:@"SelectMutantCell" bundle:nil]; 
    [self.tableView registerNib:cellNib forCellReuseIdentifier:@"SelectMutantCell"]; 

} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"SelectMutantCell"; 

    SelectMutantCell *cell = (SelectMutantCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    NSLog(@"---"); 

    cell.nameLabel.text = @"Hehe"]; 
    cell.descriptionLabel.text = @"hhe"; 



    return cell;  
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 3; 
} 

的的NSLog(@ 「---」)是調試和運行時出現。問題是在

cell.nameLabel.text = @"Hehe"]; 
cell.descriptionLabel.text = @"hhe"; 

這一塊,如果我寫cell.textLabel.text =「測試」它的工作原理,但我需要我的自定義單元格所示。

任何幫助將不勝感激..

+0

確認SelectMutantCell.xib中的表視圖單元格實際上具有自定義類「SelectMutantCell」。 – 2012-08-03 11:18:06

+0

http://oi50.tinypic.com/2hg4wpd.jpg在這裏你可以看到 – sftsz 2012-08-03 11:21:17

回答

2

如果插入(細胞==零){}循環

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"SelectMutantCell"; 
    SelectMutantCell *cell = (SelectMutantCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[SelectMutantCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    NSLog(@"---"); 
    cell.nameLabel.text = @"Hehe"]; 
    cell.descriptionLabel.text = @"hhe"; 
    return cell;  
} 
+0

沒有沒有工作。我也把 UINib * cellNib = [UINib nibWithNibName:@「SelectMutantCell」bundle:nil]; [self.tableView registerNib:cellNib forCellReuseIdentifier:@「SelectMutantCell」]; in viewDidLoad。但那也不管用。順便說一句,即時通訊使用故事板和ARC – sftsz 2012-08-03 11:03:06

+0

@ user431791:這不應該是必要的。如果一個包含表視圖單元格的nib向'registerNib:forCellReuseIdentifier:'註冊,那麼'dequeueReusableCellWithIdentifier:'將**總是返回一個單元格 - 或者從隊列中或者從nib文件實例化的新單元格中返回。 – 2012-08-03 11:03:45

+0

是的,所以我不認爲這是問題也不解決。 – sftsz 2012-08-03 11:05:46

0

看着你cellForRowAtIndexPath:方法,你從來沒有初始化的單元格。嘗試這樣做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *CellIdentifier = @"SelectMutantCell"; 

    SelectMutantCell *cell = (SelectMutantCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SelectMutantCell" owner:nil options:nil]; 
     for(id currentObject in topLevelObjects) { 
      if([currentObject isKindOfClass:[SelectMutantCell class]]) { 
       cell = (SelectMutantCell *)currentObject; 
       break; 
      } 
     } 
    } 
    NSLog(@"---"); 

    cell.nameLabel.text = @"Hehe"]; 
    cell.descriptionLabel.text = @"hhe"; 



    return cell;  
} 
+0

同樣的問題沒效果。獲得空行stil .. – sftsz 2012-08-03 11:04:59

+0

您的電池插座是否正確連接? – Alexander 2012-08-03 11:16:10

+0

我這麼認爲,我怎麼能確定呢?即時通訊看故事板上的連接引用,他們似乎沒問題。如果你想我可以上傳項目到某個地方..如果它更容易解決... – sftsz 2012-08-03 11:17:35

0

我有同樣的問題。爲了解決這個問題檢查以下內容:在你的筆尖文件

  • ,您已刪除默認創建的根視圖,並在Xcode對象瀏覽器拖一個UITableViewCell,然後設置的UITableViewCell的自定義類型爲子類SelectMutantCell

  • 已鏈接已添加的小區標識,以SelectMutantCell網點

  • ,因爲你會在故事板

  • 最後做但並非最不重要的是,您沒有在故事板TableView中添加相同的單元格,它會在故事板中的單元格與[self.tableView registerNib:cellNib forCellReuseIdentifier:@「SelectMutantCell」]之間混淆。