繁体   English   中英

滚动时的UITableView单元格更新

[英]UITableView Cell Updates When I Scroll

我正在开发iPhone应用程序,但是有一个小问题,或者我认为。 我有一个tableview,我通过返回方法中的单元格来设置单元格的颜色和标签。 当我运行它时,一切似乎都很好,问题是当我将单元格的颜色更改滚动到下面或上面的单元格的颜色时。 我不确定该如何解决。 我认为由于某种原因,它会在更新单元格时进行更新 我如何将其更改为仅在开始时设置单元格属性,而不是在滚动时设置?

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
                                     reuseIdentifier:SimpleTableIdentifier];

        UIColor * lightRed = [UIColor colorWithRed:0.6 green:0.1 blue:0 alpha:1.0];
        UIColor * darkRed = [UIColor colorWithRed:0.7 green:0.1 blue:0 alpha:1.0];
        UIColor * darkGray = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1.0];

        NSLog(@"A");

        if (tableCellCount % 2 == 0) {
            cell.contentView.backgroundColor = darkRed;
            cell.detailTextLabel.backgroundColor = darkRed;
            cell.textLabel.backgroundColor = darkRed;
        } else {
            cell.contentView.backgroundColor = lightRed;
            cell.detailTextLabel.backgroundColor = lightRed;
            cell.textLabel.backgroundColor = lightRed;
        }
        cell.textLabel.textColor = [UIColor whiteColor];

        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:darkGray];
        [cell setSelectedBackgroundView:bgColorView];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];

    return cell;
}

更新资料

我想出了解决我问题的方法。

这是更新的代码:

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

    static NSString *CellIdentifier = @"Cell";    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                      reuseIdentifier:CellIdentifier];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"] 
                           objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"News_Icon@2x.png"];

    UIColor * lightRed = [UIColor colorWithRed:0.7 green:0.1 blue:0 alpha:1.0];
    UIColor * darkRed = [UIColor colorWithRed:0.6 green:0.1 blue:0 alpha:1.0];
    UIColor * darkGray = [UIColor colorWithRed:0.15 green:0.15 blue:0.15 alpha:1.0];

    if (row % 2 == 0) {
        cell.contentView.backgroundColor = darkRed;
        cell.detailTextLabel.backgroundColor = darkRed;
        cell.textLabel.backgroundColor = darkRed;
    } else {
        cell.contentView.backgroundColor = lightRed;
        cell.detailTextLabel.backgroundColor = lightRed;
        cell.textLabel.backgroundColor = lightRed;
    }
    cell.textLabel.textColor = [UIColor whiteColor];

    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:darkGray];
    [cell setSelectedBackgroundView:bgColorView];

    for(UIView *view in [tableView subviews]) {
        if([[[view class] description] isEqualToString:@"UITableViewIndex"]) {
            [view setBackgroundColor:[UIColor clearColor]];
        }
    }
    return cell;
}

对于表中的每个单元格,只有在创建该单元格时才需要设置第一次的属性,并且每次都需要设置其他属性。

UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:anyId];
if(cell==nil)
{
    // Properties to set the first time when the cell is created only
}
// Properties to set each time

return cell;

您正在重用单元格视图。 检查表视图控制器tableView:cellForRowAtIndexPath: ,您确定正在使用dequeueReusableCellWithIdentifier: 您必须为该重复使用的单元格设置颜色值。

您在重新使用单元格时遇到问题。 UITableViews回收单元格。 因此,如果您未在tableview:cellForRowAtIndex:方法中正确处理此问题,则滚动时会得到奇怪的结果。 始终假定您正在设置的单元可能已经被设置为另一个单元。

暂无
暂无

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

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