UITableView滚动和重绘问题 [英] UITableView scrolling and redraw issue

查看:89
本文介绍了UITableView滚动和重绘问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果在自定义单元格中添加了一些图像和子视图,则必须重用该单元格,以便自定义控件不会出现在其他单元格上,但是这里有其他问题.我只想在第一部分的第一个单元格上有ImageView,所以我在以下代码中使用了IndexPath.Section == 0和IndexPath.Row == 0条件,但是问题是当我滚动表时,另一个单元格会满足此条件,并且我的代码也会在该单元格上创建imageview.我已经尝试过标记它并使用相同的标记cellView,但是它也没有帮助.单元问题是禁用少数单元的用户交互.最终,在滚动之后,它将禁用所有单元的用户交互.反正有解决办法吗?

I know that if I have some images and subviews added in customized cell then I have to reuse the cell so that custom control won't appear on other cells but here I have other issue. I just want to have ImageView on first cell of first section so I have used IndexPath.Section==0 and IndexPath.Row==0 condition in following code but the problem is when I scroll table, the other cell will meet this condition and my code will create imageview on that cell as well. I have tried Tagging it and using same tagged cellView but it didn't help either. The cell issue is with disabling user interactions for few cells. Eventually after scrolling it disables user interactions for all cells. Is there anyway to resolve this?

谢谢.

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

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

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

if(indexPath.section == 0 && indexPath.row == 0) {
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
    UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease];
    [imageView setFrame: CGRectMake(10, 10, 54, 54)];
    [cellView addSubview:imageView];
    cell.backgroundView = cellView;

    return cell;
} else if(indexPath.row == 0) {
    NSString * title = [NSString string];
    switch (indexPath.section) {
        case 1:
            title = @"Friends";
            break;
        case 2:
            title = @"Accounts";
            break;
        case 3:
            title = @"Stats";
            break;
        default:
            title = nil;
            break;
    }
    cell.textLabel.text = title;
    cell.userInteractionEnabled = NO;
    return cell;
}

cell.textLabel.text = @"Test";
return cell;
}

[已解决]正确的代码:

[RESOLVED] Correct code:

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

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

if (cell == nil) 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

if(indexPath.section == 0 && indexPath.row == 0) {
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
    cell.imageView.image = imageView.image;
    cell.textLabel.text = nil;
    cell.textLabel.textColor = [UIColor clearColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = YES;
    return cell;
} else if(indexPath.row == 0) {
    NSString * title = [NSString string];
    switch (indexPath.section) {
        case 1:
            title = @"Friends";
            break;
        case 2:
            title = @"Accounts";
            break;
        case 3:
            title = @"Stats";
            break;
        default:
            title = nil;
            break;
    }

    cell.imageView.image = nil;
    cell.textLabel.text = title;
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.userInteractionEnabled = NO;

    return cell;
}


cell.imageView.image = nil;
cell.textLabel.text = [cellItems objectAtIndex:(rows+indexPath.row-1)];
cell.textLabel.textColor = [UIColor blueColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.userInteractionEnabled = YES;
return cell;
}

[验证码]

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

static NSString *NormalCellIdentifier = @"NormalCell";
static NSString *TitleCellIdentifier = @"TitleCell";
NSString *neededCellType;

if(indexPath.section == 0 && indexPath.row == 0) {
    neededCellType = TitleCellIdentifier;
} else {
    neededCellType = NormalCellIdentifier;
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];

    //Only add content to cell if it is new
    if([neededCellType isEqualToString: TitleCellIdentifier]) {
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease];
        cell.imageView.image = imageView.image;
    }
}

if([neededCellType isEqualToString: NormalCellIdentifier]) {
    NSString * title;
    if(indexPath.row == 0) {
        switch (indexPath.section) {
            case 1:
                title = @"Friends";
                break;
            case 2:
                title = @"Accounts";
                break;
            case 3:
                title = @"Stats";
                break;
            default:
                title = nil;
                break;
        }
        cell.textLabel.text = title;
        cell.textLabel.textColor = [UIColor redColor];
        cell.userInteractionEnabled = NO;
    } else {

        cell.userInteractionEnabled = YES;
        cell.textLabel.textColor = [UIColor blueColor];
        cell.textLabel.text = @"Test";
    }
}

return cell; 
}

推荐答案

问题是您不允许以后正确重用正确显示图像的单元格,并且图像视图仍在其中.

The problem is that you are not allowing for the possibility that the cell that was correctly showing the image gets reused later and the image view is still in there.

以下是两种解决方案:

  1. 在创建图像视图时设置其标签值,然后在设置单元格时包括代码以检查并在必要时删除旧的imageView.

  1. set the tag value of the image view when you create it, then when you setup the cells, include code to check for and remove the old imageView if necessary.

为需要图像视图的单元和不需要图像视图的单元分配不同的重用标识符.然后,请确保仅在创建单元时才向它们添加新的图像视图,而不是在重用它们时向它们添加新的图像视图.

assign different reuse identifiers to cells that need an image view and those that do not. Then make sure that you are only adding a new image view to cells when they are being created and not when they are being reused.


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

static NSString *NormalCellIdentifier = @"NormalCell"; static NSString *TitleCellIdentifier = @"TitleCell"; NSString *neededCellType;

static NSString *NormalCellIdentifier = @"NormalCell"; static NSString *TitleCellIdentifier = @"TitleCell"; NSString *neededCellType;

if(indexPath.section == 0 && indexPath.row == 0) { neededCellType = TitleCellIdentifier; } else { neededCellType = NormalCellIdentifier; }

if(indexPath.section == 0 && indexPath.row == 0) { neededCellType = TitleCellIdentifier; } else { neededCellType = NormalCellIdentifier; }

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:neededCellType];

if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];

if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:neededCellType] autorelease];

//Only add content to cell if it is new if([neededCellType isEqualToString: TitleCellIdentifier]) { UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease]; [imageView setFrame: CGRectMake(10, 10, 54, 54)]; [cellView addSubview:imageView]; cell.backgroundView = cellView; } }

//Only add content to cell if it is new if([neededCellType isEqualToString: TitleCellIdentifier]) { UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"me.jpg"]] autorelease]; UIView *cellView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320,132)] autorelease]; [imageView setFrame: CGRectMake(10, 10, 54, 54)]; [cellView addSubview:imageView]; cell.backgroundView = cellView; } }

if([neededCellType isEqualToString: NormalCellIdentifier]) { NSString * title; if(indexPath.row == 0) {

if([neededCellType isEqualToString: NormalCellIdentifier]) { NSString * title; if(indexPath.row == 0) {

switch (indexPath.section) {
    case 1:
        title = @"Friends";
        break;
    case 2:
        title = @"Accounts";
        break;
    case 3:
        title = @"Stats";
        break;
    default:
        title = nil;
        break;
}
cell.textLabel.text = title;
cell.userInteractionEnabled = NO;

}

(最后几行从代码框中掉了).那应该做.

(Those last few lines fell out of the code box). That should do it.

这篇关于UITableView滚动和重绘问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆