2011-08-06 91 views
0

我想添加一個customCell,它只是讓我左對齊一個字符串,然後右對齊另一個。在之前的問題中,有人提出了以下建議,但我在使用代碼時遇到問題。 Xcode中說:RootViewController的可能不--contentView響應,並調用[自我內容查看] addSubView:項目]或[自內容查看] addSubView:等級]會崩潰我的應用程序在運行時定製單元格遇到問題UITableView

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

     static NSString *CellIdentifier = @"Cell"; 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

      UILabel *rank = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20]; 
      //Mess around with the rects, I am just merely guessing. 
      [rank setTag:5]; 
      [[self contentView] addSubView:rank]; 
      [rank release]; 


      UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 220, 20]; 
      //Important 
      [item setTextAlignment:UITextAlignmentRight]; 
      [item setTag:6]; 
      [[self contentView] addSubView:item]; 
      [item release]; 
     } 

     UILabel *rank = (UILabel *)[cell viewWithTag:5]; 
     UILabel *item = (UILabel *)[cell viewWithTag:6]; 

     rank = @"leftside"; 
     item = @"rightside"; 

    } 

感謝您的任何想法

回答

1

你應該看

[cell.contentView addSubview:item]; 
[cell.contentView addSubview:rank]; 

rank.text = @"leftside"; 
item.text = @"rightside"; 

還有一點要注意這裏。如果你的UITableView是scrollEnabled,你會遇到cellReusability的問題,你的標籤會被後續的滾動搞亂。我建議你將子類UITableViewCells添加到佈局中,然後使用CustomUITableViewCells

+0

感謝這是問題,但正如你指出的滾動上/下似乎打破了一切。您使用CustomUITableViewCells描述的任何示例? – Hoofamon

1

self這裏是控制器。接收者不應使用self,而應該將contentView發送給您剛創建的單元格。