简体   繁体   English

iPhone + UITableView +格式单元格

[英]iPhone + UITableView + format cells

I am using following method in my application: 我在应用程序中使用以下方法:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        cell.contentView.alpha = 0.5;
    }
}   

When I run the application I have 7 rows in my table. 当我运行该应用程序时,我的表中有7行。 According to above function only the cell of first row (row number 0) should be formatted (because of the if condition). 根据上述功能,仅应格式化第一行(行号0)的单元格(由于if条件)。

Cell of 1st row (row number 0) is formatted properly (as per desired output). 第一行的单元格(行号0)的格式正确(根据所需的输出)。 But if I scroll the table down one more cell is displayed as formatted: cell at row number 5. 但是,如果我向下滚动表格,则将以格式显示另外一个单元格:第5行的单元格。

Why so? 为什么这样?

I think the reason is that TableView reuses already existing cells and displays them if it is possible. 我认为原因是TableView重用了已经存在的单元并在可能的情况下显示它们。 What happens here - when table is scrolled and row 0 becomes invisible then itscorresponding cell is used for a newly displayed row. 此处发生的情况-滚动表并使第0行变为不可见时,其对应的单元格将用于新显示的行。 So if you're reusing cells you must reset their properties: 因此,如果要重用单元格,则必须重置其属性:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
   if(indexPath.row == 0) { 
       cell.contentView.backgroundColor = [UIColor lightGrayColor];  
       cell.contentView.alpha = 0.5; } 
   else
   {
    // reset cell background to default value
   }
}

I agree with Vladimir's answer. 我同意弗拉基米尔的回答。 However, i also believe you should follow a different approach. 但是,我也相信您应该采用不同的方法。

In the current situation you are formatting your cell frequently, as the method gets called in each scroll, and this leads you to a suboptimal performance. 在当前情况下,由于每次滚动都会调用该方法,因此会频繁地格式化单元格,这会导致性能欠佳。

A more elegant solution is to format 1st row differently than others only "once" : when you create your cells. 一个更优雅的解决方案是,在创建单元格时,将第一行的格式设置为与其他“仅一次”格式不同:

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

        static NSString *CellIdentifier;
        if(indexPath.row == 0)
        CellIdentifier = @"1stRow";
        else
        CellIdentifier = @"OtherRows";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell==nil) { 
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            if(indexPath.row == 0){
                cell.contentView.backgroundColor = [UIColor lightGrayColor];  
                cell.contentView.alpha = 0.5;
                    // Other cell properties:textColor,font,...
            }
            else{
                cell.contentView.backgroundColor = [UIColor blackColor];  
                cell.contentView.alpha = 1;
                //Other cell properties: textColor,font...
            }

        }
        cell.textLabel.text = .....
        return cell;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM