删除单元格后刷新单元格内容 [英] refreshing cell content after deleting cells

查看:64
本文介绍了删除单元格后刷新单元格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

我有一个UITableView,其中包含一些文本和一些图像.单元格的TextLabel显示文本,然后将带有一些UIImageViews的UIView作为其子视图添加到单元格contentview.

I have a UITableView with some Text and some Images as content. The TextLabel of the cell displays the text and I add a UIView with some UIImageViews as its subview to the cells contentview.

一切正常,直到删除一些单元格为止.发生的事情是,我从表中删除了一个单元格(让我们说第一个),重新加载其数据并例如然后第二个单元格又向上移动一个!右侧的UIView包含第一个(已删除)单元格中的内容.

Everything works fine until I delete some cells. What happens is, I delete a cell (lets say the first) from the table, reload its data and e.g. then second cell moves one further up BUT! the UIView to the right contains the content from the first (deleted) cell.

我不知道为什么会发生这种情况-尽管我认为cellForRowAtIndexPath回调方法出了点问题.

I dont know why this happens - though I assume somethings wrong in me cellForRowAtIndexPath callback method.

我将粘贴一些代码以使其更清晰.

I'll paste some code to make it clearer.

我能做的是卸载控制器并再次加载-然后图像再次包含正确的内容....

What I can do, is unload the controller and load it again - then the images contain the correct content again....

这里是我的代码:

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



static NSString *MyIdentifierUNCHECK = @"MyIdentifierUNCHECK";


MyStuff *m = [allObjects objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:MyIdentifierUNCHECK];
    UIView *v;    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifierUNCHECK] autorelease];
        v = [[[UIView alloc] initWithFrame:CGRectMake(5.0, 5.0, 70.0, 70.0)] autorelease];

        v.contentMode = UIViewContentModeScaleAspectFill;
        [v addSubview:[[UIImageView alloc] init]];//some image view
        [cell.contentView addSubview:v];
    }

    cell.textLabel.text = m.name;
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:14.0];
    cell.imageView.image = [UIImage imageNamed:@"checkbox.png"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;

推荐答案

我假设右边的视图"是指您在其中为您的单元格实例化的视图...此处出现问题是因为您重复使用单元格并没有为每个单元格重置UIView,如果您有足够的单元格可以滚动浏览它们,就会看到同样的问题.当您重复使用单元格时,必须始终假定它们包含脏数据,并应使用正确的数据重新加载它们,在这种情况下,您的错误在于以下代码段:

I am assuming by "view in the right" you mean the view that u instanciate there for your cell...The problem here occurs because you reuse cells and are not resetting the UIView for each cell, you will see your same problem occur if you have enough cells that you can scroll through them. When you reuse cells you must always assume they have dirty data and should reload them with the right data, in your case, your mistake is in this snippet:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifierUNCHECK] autorelease];
    v = [[[UIView alloc] initWithFrame:CGRectMake(5.0, 5.0, 70.0, 70.0)] autorelease];

    v.contentMode = UIViewContentModeScaleAspectFill;
    [v addSubview:[[UIImageView alloc] init]];//some image view
    [cell.contentView addSubview:v];
}

您只是在第一次分配单元格时实例化该视图,所以这是正在发生的事情...1-您首先制作所有单元格并进行正确查看"2-您删除一个单元格3-对于第二个单元格,系统正在重用作为第一个单元格创建的UITableViewCell4-由于单元格不是nil,所以没有设置UIView,您看到的是第二个单元格的第一个单元格UIView

You are only instantiating the view in the cell when its first allocated, so this is whats happening... 1- You Make all your cells initially and make their "right view" 2- You delete a cell 3- For your second cell, the system is reusing the UITableViewCell that you created as your first cell 4- Since the cell isnt nil the UIView isnt being set and what you see is the first cells UIView for the second cell

由于您没有重置单元格UIView,因此您在单元格中看到的视图错误,而不是您期望的视图.

Since you are not resetting the cells UIView, you are seeing the wrong view in the cell instead of the one you expect.

要解决此问题,您可以选择不重复使用单元格,或者可以移动添加UIView的代码,以在每次调用cellForRowAtIndexPath时发生,这样,正确的UIView就会加载正确的单元格

In order to fix this you can either choose not to reuse cells OR you can move the code that adds the UIView to occur everytime cellForRowAtIndexPath is called, that way the right UIView will be loaded with the right cell

这篇关于删除单元格后刷新单元格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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