2010-04-23 49 views
0

我試圖使用自定義UITableViewCell構建我的應用程序。這是我UIViewController,增加了viewCell表的代碼:添加自定義UITableViewCell崩潰模擬器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"------- Tableview --------"); 

    static NSString *myIdentifier = @"MyIdentifier"; 

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier]; 
    if(cell == nil) { 
     NSArray *cellView = [[NSBundle mainBundle] loadNibNamed:@"tblCellView" owner:self options:nil]; 
     cell = [cellView objectAtIndex:0]; 
    } 

    [cell setLabelText:[NSString stringWithFormat:@"indexpath.row: %d", indexPath.row]]; 


    //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myIdentifier] autorelease]; 
    cell.textLabel.text = @"bös"; 
    return cell; 
    } 

如果我去掉上面的「返回細胞」行返回一個普通UITableViewCell沒有任何錯誤,但只要我嘗試執行我定製單元它崩潰與此錯誤:

------- Tableview -------- 2010-04-23 11:17:33.163 Golf[26935:40b] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-984.38/UITableView.m:4709 2010-04-23 11:17:33.164 Golf[26935:40b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 2010-04-23 11:17:33.165 Golf[26935:40b] Stack: (

...

我已經配置.xib文件應與適當的插座應該。和UITableViewCell的標識符對應於名稱即時嘗試從NSBundle加載

+0

試用鑄造(MyTableCell *) – willcodejavaforfood 2010-04-23 11:32:47

+0

不要忘記導入自定義單元格英寸m文件並與自定義單元格綁定,並檢查數據源的tableview代表。 – 2012-09-14 08:37:39

回答

1

您還應該確保將Interface Builder中的自定義類類型設置爲相應的類名(在您的示例中爲MyTableCell),否則投射也不起作用。

+0

這是一個老問題,但我相信這是解決我的問題的方法,所以我將其標記爲答案。 – nevva 2012-04-24 09:01:13

+0

我有一個類似的問題,這個答案讓我找到它。我忘記將文件所有者的類型設置爲我的iPhone xib中的視圖控制器,因此我只在iPhone上崩潰。 – 2012-08-25 21:31:32

1

我看不到tblCell宣佈,這可能是您的代碼的許多問題之一。首先,您需要執行以下操作:

if (cell == nil) { 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil]; 
    cell = (MyTableCell *)[nib objectAtIndex:0]; 
} 

您只是加載筆尖,但從不從中提取單元格。

--Update--

您也應該檢查IB您的自定義單元格,如果有任何錯誤或不正確的綁定。

+0

很抱歉,那是舊代碼。我現在更新了我原來的問題。 – nevva 2010-04-23 09:36:57

+0

嘗試將其轉換爲(MyTableCell *) – willcodejavaforfood 2010-04-23 11:31:58

0

我認爲loadNibNamed: nethod由於某種原因返回nil。因此導致的細胞對象也是零。

1

我創造了這個定義,以幫助


#define LoadViewNib(__NIBNAME__) [[[NSBundle mainBundle] loadNibNamed:__NIBNAME__ owner:self options:nil] objectAtIndex:0] 

使用,你只能做這個



if(cell == nil) { 

     cell = LoadViewNib(@"MyCellNibName"); 
} 

0

參考:如果您加載自己的自定義單元格,然後A Closer Look at Table-View Cells

「爲此過程的編程部分設置的一個重要屬性是每個對象的標籤屬性。在Attrib的View部分中查找此屬性utes窗格併爲每個對象分配一個唯一的整數。「

立即檢索您的標籤這樣 (我想你指定標籤= 1到標籤)

UILabel *cellLabel = (UILabel *)[cell viewWithTag:1]; 
cellLabel.text = @"Your text goes here"