2011-08-06 88 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发送给您刚创建的单元格。