2012-12-27 9 views
1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 

    NSString *currentNames = [nameKeys objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:currentNames]; 

    return cell; 
    } 

爲什麼我得到低於錯誤(我正在使用.XIB而不是故事板iOS 6和Xcode 4.5)。 我確實已經連接datasource並從連接檢查器或文件所有者處委託。如何在iOS 6和xcode 4.5中使用.xib或nib來製作簡單的表格視圖?



錯誤:

Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:4460 
    2012-12-27 11:35:45.146 TableViewWithXib[570:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 
+0

看看你的視圖Controller是從哪個類繼承的?即。在.h文件中的這種行'@interface YOURCLASSNAME:UIViewController ' –

+0

是的,我已經確認了的協議,但錯誤仍然相同 –

+0

然後,你必須有一個'@property強,非原子)IBOutlet UITableView * tableView;'定義的屬性,也在.XIB中連接到'UITableView'實例。然後它不會給出任何錯誤,因爲它從不給我任何錯誤。 –

回答

1

按照docs

You must register a class or nib file using the 
registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: 
method before calling, 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

因此,如果你不這樣做,用筆尖文件,然後使用

UITableViewCell *cell = [tableView 
dequeueReusableCellWithIdentifier:CellIdentifier]; 

即;沒有forIndexPath:indexPath也可以。

相關問題