简体   繁体   English

滚动时的UITableView单元格更新

[英]UITableView Cell Updates When I Scroll

I'm developing an iPhone Application, and I have a small issue, or so I think. 我正在开发iPhone应用程序,但是有一个小问题,或者我认为。 I have a tableview and I got it to set the colors and labels of the cells by returning the cells in a method. 我有一个tableview,我通过返回方法中的单元格来设置单元格的颜色和标签。 Everything seems fine when I run it, the problem is when I scroll the cell's color changes to that of the cell below or above. 当我运行它时,一切似乎都很好,问题是当我将单元格的颜色更改滚动到下面或上面的单元格的颜色时。 I'm not sure how to solve this. 我不确定该如何解决。 I believe it's updating the cell when I scroll for some reason. 我认为由于某种原因,它会在更新单元格时进行更新 How would I change this to only set the cell properties at first and not when I scroll? 我如何将其更改为仅在开始时设置单元格属性,而不是在滚动时设置?

- (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;
}

Update 更新资料

I figured out the solution to my problem. 我想出了解决我问题的方法。

Here's the updated code: 这是更新的代码:

- (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;
}

For each cell in the table, there are properties you want to set the first time only when the cell gets created and there are other properties you want to set each time. 对于表中的每个单元格,只有在创建该单元格时才需要设置第一次的属性,并且每次都需要设置其他属性。

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;

You are reusing cell views. 您正在重用单元格视图。 Check the method in your table view controller tableView:cellForRowAtIndexPath: and you for sure are using dequeueReusableCellWithIdentifier: . 检查表视图控制器tableView:cellForRowAtIndexPath: ,您确定正在使用dequeueReusableCellWithIdentifier: You have to set color values to that reused cell. 您必须为该重复使用的单元格设置颜色值。

You have a problem with cells being reused. 您在重新使用单元格时遇到问题。 UITableViews recycle cells. UITableViews回收单元格。 So if you are not correctly handling this in your tableview:cellForRowAtIndex: method you will get strange results when you scroll. 因此,如果您未在tableview:cellForRowAtIndex:方法中正确处理此问题,则滚动时会得到奇怪的结果。 Always assume that the cell you are setting up may already be setup as another cell. 始终假定您正在设置的单元可能已经被设置为另一个单元。

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

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