2

我有一个UITableView不同的部分和行。我想在每个单元格附近有不同的标签或图像,例如图标,但我不知道为什么它看起来像这张图片:

滚动也改变了标签的大小!

这是代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.backgroundColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:label];

}
else if(indexPath.row == 1)
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:label];    }
else if(indexPath.row == 2)
{
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:label];    }
else{

}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
4

2 回答 2

0

在您的代码中,当用户滚动表格时,标签创建了很多次!您必须检查单元格中是否已存在颜色标签,然后设置颜色

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int colorLabelTag = 9999;
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
                                  reuseIdentifier:CellIdentifier];     
}

// Because table cells are reusing we must check is label already in cell
UILabel* label = (UILabel *) [cell viewWithTag:colorLabelTag];
// If no - we create label
if (!label) {
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(3,0,40,40)];
    label.tag = colorLabelTag;
    [cell addSubview:label];
}

if(indexPath.row == 0)
{
    label.backgroundColor = [UIColor darkGrayColor];        
}
else if(indexPath.row == 1)
{
    label.backgroundColor = [UIColor redColor];
}
else if(indexPath.row == 2)
{
    label.backgroundColor = [UIColor redColor];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
于 2012-08-07T09:25:06.163 回答
0

除非您想要比这更多的自定义,否则不需要对单元格进行子类化。让我试一试:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
    NSArray *contents = [[self sectionContents] objectForKey:key];
    NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
    reuseIdentifier:CellIdentifier];
        // If you need to add more custom views then create them here. E.g.: 
        UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(10,200,20,20)];
        myLable.text = @"Some Value";
        label.tag = 101; // use anything but 1. Better use a constant expression.
        [cell.contentView addSubview:myLable];
    }
    switch (indexPath.row) {
      case 1: 
        cell.imageView.image = [UIImage imageNamed:@"red.png"];
        break;

      case 2: 
        cell.imageView.image = [UIImage imageNamed:@"blue.png"];
        break;

      case 3: 
        cell.imageView.image = [UIImage imageNamed:@"green.png"];
        break;
    }  

   UILabel* theLabel = (UILabel *) [cell viewWithTag:101]; // again, a constant is of better style here. 
   if (theLabel  { // it must exist. However, it is good practice to check upon that. 
    label.text = @"someOtherValue";
   }

    [[cell textLabel] setText:contentForThisRow];

    // you may need to layout your subviews here. 

    return cell;
}
于 2012-08-07T12:07:05.513 回答