2013-06-20 47 views
0

我已經梳理了堆棧溢出和其他網站上的幾十個問題/答案,但我似乎無法得到它的工作。它看起來像一個相當普遍的問題,但我發現其他問題的答案都沒有應用。基本上,我試圖添加一個簡單的UITextView到一個表格單元格中,但它沒有顯示出來。這是我得到的:UITableViewCell不顯示子視圖UITextView

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    UITextView *textviewFactoid = nil; 

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

     textviewFactoid = [[UITextView alloc]initWithFrame:CGRectMake(0, -3, 300, 25)]; 
     [textviewFactoid setFont:[UIFont systemFontOfSize:12.0f]]; 
     [textviewFactoid setBackgroundColor:[UIColor blueColor]]; 
     [textviewFactoid setTextColor:[UIColor grayColor]]; 
     [textviewFactoid setScrollEnabled:NO]; 
     [textviewFactoid setEditable:NO]; 
     [textviewFactoid setTag:98]; 
     [[cell contentView] addSubview:textviewFactoid]; 
    } else { 
     textviewFactoid = (UITextView*)[cell.contentView viewWithTag:98]; 
    } 

    NSString *textviewFactoidText = @"Here is the factoid"; 
    [textviewFactoid setText:textviewFactoidText]; 

    return cell; 
} 

有沒有人知道爲什麼細胞會變成空白?我將backgroundColor設置爲藍色,這樣我就可以看到textview是否存在,而且看起來似乎沒有。我也沒有收到任何錯誤或警告,以幫助我找出錯誤。

我已經複製了這段代碼幾乎逐字從我寫的另一個應用程序,它在那裏工作正常...我確定這是簡單的,我失蹤或我改變了,但我是種難倒。

任何人都比我更尖銳的眼睛?

回答

2

如果您正在爲iOS 6構建並且已經爲您的單元格標識符註冊了一個筆尖或類,則dequeueReusableCellWithIdentifier將始終返回一個單元格。您的if (cell == nil) {永遠不會評估爲真,並在該塊內運行代碼。

+2

是的,如果在你的if塊中存在一個斷點,可能性不大,因爲在這個答案中聲明的原因沒有被調用。 – Idles

+2

是的,在Interface Builder中使用自定義的'UITableViewCell'類更好。 –

+1

夠公平的。現在我開始學習如何製作一個自定義的Cell類。謝謝你們! – Nerrolken