2011-05-12 77 views
0

我有我創建的自定義TableViewCell。每個單元格都有兩個我想要變成不同顏色的UI標籤。此外,我有我的tableView的一部分,我想有不同的背景顏色和文字顏色。重寫uitableviewcell屬性有困難

現在,我無法使用我的UILabel.textColor獲取文本顏色;雖然我確實讓它與cell.textLabel.textColor一起工作。此外cell.background顏色不起作用。

如果我在底部的switch語句中執行了單元格屬性更改,但單元格屬性不會正確取出,因此隨着上下滾動,隨機單元格將變爲白色文本紅色背景。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"MyIdentifier"; 

    UILabel* itemLabel; 
    UILabel* amountLabel; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    itemLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 35.0)] autorelease]; 
    itemLabel.tag = ITEMLABEL_TAG; 
    itemLabel.font = [UIFont systemFontOfSize:14.0]; 
    itemLabel.textAlignment = UITextAlignmentLeft; 
    itemLabel.autoresizingMask = UIViewAutoresizingNone; 
    itemLabel.backgroundColor = [UIColor clearColor]; 
    itemLabel.textColor = [UIColor blueColor]; //This doesn't work 

    if (indexPath.section == 4) 
    { 
     cell.textLabel.textColor = [UIColor whiteColor]; //This works 
     cell.backgroundColor = [UIColor redColor]; //This doesn't work 
    } 
    [cell.contentView addSubview:itemLabel]; 

    cell.backgroundColor = [UIColor whiteColor]; 

    amountLabel = [[[UILabel alloc]initWithFrame:CGRectMake(225.0, 0.0, 55.0, 35.0)] autorelease]; 
    amountLabel.tag = AMOUNTLABEL_TAG; 
    amountLabel.font = [UIFont systemFontOfSize:14.0]; 
    amountLabel.textAlignment = UITextAlignmentLeft; 
    amountLabel.textColor = [UIColor blackColor]; //This doesn't work 
    amountLabel.backgroundColor = [UIColor clearColor]; 
    amountLabel.autoresizingMask = UIViewAutoresizingNone; 
    [cell.contentView addSubview:amountLabel]; 


    switch (indexPath.section) { 
     case 0: 
      cell.textLabel.text = [monthlyExpenses objectAtIndex:indexPath.row]; 
      break; 
     case 1: 
      cell.textLabel.text = [oneTimeFees objectAtIndex:indexPath.row]; 
      break; 
     case 2: 
      cell.textLabel.text = [renters objectAtIndex:indexPath.row]; 
      break; 
     case 3: 
      cell.textLabel.text = [travelExpenses objectAtIndex:indexPath.row]; 
      break; 
     case 4: 
      cell.textLabel.text = @"Generate Report"; 
      break; 
    } 

return cell; 
} 

回答

0

您將需要更改單元格的背景顏色在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

在這裏,你可以做定製表格顯示單元之前。