0

我已經從NIB創建了一個自定義的UITableViewCell。當rowHeight沒有被設置時,所有東西都會出現(儘管一切都被壓扁了)。我不知道如果我再利用和妥善創建細胞:UITableViewCell不能正常顯示/從NIB中正確地重複使用

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"ARCell"; 
    ARObject *myARObject; 
    myARObject = [items objectAtIndex:indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil]; 
     cell = arCell; 
     self.arCell = nil; 
    } 

    UILabel *label; 
    label = (UILabel *)[cell viewWithTag:0]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.username]; 

    label = (UILabel *)[cell viewWithTag:1]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.created_at]; 

    label = (UILabel *)[cell viewWithTag:2]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.articleTitle]; 

    label = (UILabel *)[cell viewWithTag:3]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.intro_text]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ CGFloat rowHeight = 105; 
    return rowHeight; 
} 

問題:

  1. 不是當rowHeight時設置的所有標籤出現。如果沒有設置rowHeight時,標籤被壓扁
  2. 當rowHeight時未設置,選擇一個項目顯示了所有標籤的

回答

0

刪除else子句

+0

問題仍然存在 – 2010-06-08 17:11:22

0

你叫loadNibNamed,但不存儲結果無處不在不明白爲什麼你的代碼在所有工作...總之,寫瞎我會嘗試這樣的事:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"ARCell"; 
    ARObject *myARObject = [items objectAtIndex:indexPath.row]; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ARCell" owner:self options:nil]; 
     cell = (UITableViewCell *)[nib objectAtIndex:0]; 
    } 

    UILabel *label; 
    label = (UILabel *)[cell viewWithTag:0]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.username]; 

    label = (UILabel *)[cell viewWithTag:1]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.created_at]; 

    label = (UILabel *)[cell viewWithTag:2]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.articleTitle]; 

    label = (UILabel *)[cell viewWithTag:3]; 
    label.text = [NSString stringWithFormat:@"%@", myARObject.intro_text]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tblView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 105.0; 
} 
+0

我得到同樣的結果。而不是cell =(UITableViewCell *)[nib objectAtIndex:0];我將它分配給我的h文件中的UITableViewCell屬性arCell – 2010-06-08 17:23:37

+0

我遵循Apple TableView編程指南中的示例:http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/ TableViewCells/TableViewCells.html#// apple_ref/doc/uid/TP40007451-CH7-SW1 – 2010-06-08 17:26:27

+0

選中並且示例看起來像您的代碼。然後,只需檢查您的arCell IBOutlet「,因爲您建立了插座連接,tvCell插座現在具有對從nib文件加載的單元格的引用。」 – JOM 2010-06-08 18:01:58