我的UITableView有重复的行 [英] My UITableView has duplicated rows

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

问题描述

我不确定为什么,但是我的UITableView并没有重复显示.

似乎用户滚动时添加的行(即屏幕开始时不在屏幕上的行)正在获取错误的行索引的数据.就像当一个新单元格被出队时,它所使用的单元格已被使用",但未被正确清理.

您是否需要清理"出队的单元,以使新单元不使用已创建的单元?

我的代码如下:

 <代码>-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {静态NSString * CustomCellIdentifier = @"CustomCellIdentifier";MyDayCell * cell =(MyDayCell *)[tableViewdequeueReusableCellWithIdentifier:CustomCellIdentifier];if(cell == nil){NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil];用于(笔尖中的id oneObject)如果([oneObject isKindOfClass:[MyDayCell类]])cell =(MyDayCell *)oneObject;}NSUInteger部分= [indexPath部分];NSUInteger行= [indexPath行];NSArray * thisSectionItems =(NSArray *)[self.listData objectForKey:[[NSNumber alloc] initWithInt:section]];MyDayDetails * rowData = [thisSectionItems objectAtIndex:row];//在这里设置我的单元格数据...返回单元} 

此代码有什么问题吗?

以前有没有人见过这样的东西?

解决方案

应该重复使用单元格.如果要关闭此功能,请关闭单元复用.

您的问题实际上出在您未包含的代码中.

 //在这里设置我的单元格数据... 

此代码负责完全加载表格中各行之间不同的单元格的各个方面.这些数据显示不止一次?如果需要,则需要对其进行设置;否则,请清除它.

例如:

  cell.textLabel.text = str?str:@"; 

那样,一遍又一遍地使用相同的几个单元格,并且不需要经常设置和销毁表单元格.

(正如我提到的,您可以关闭单元重用.但是您应该使这项工作有效.)

Im not sure why, but my UITableView, which isnt anything fancy, is showing repeating rows when it shouldnt be.

It seems that the rows that get added when the user scrolls (i.e. the rows that are off the screen to start with) are getting the data for the wrong row index. Its almost like when a new cell is de-queued, it's using a cell that 'was' used, but wasn't cleaned up correctly.

Do you need to 'clean up' cells that are de-queue so that new cells dont use cells that are already created?

my code is as below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CustomCellIdentifier = @"CustomCellIdentifier";

    MyDayCell *cell = (MyDayCell *)[tableView 
                                      dequeueReusableCellWithIdentifier: CustomCellIdentifier];

    if (cell == nil) { 
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil];

        for (id oneObject in nib) 
            if ([oneObject isKindOfClass:[MyDayCell class]])
                cell = (MyDayCell *)oneObject;

    } 

    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row]; 
    NSArray *thisSectionItems = (NSArray*)[self.listData objectForKey: [[NSNumber alloc] initWithInt:section]];
    MyDayDetails *rowData = [thisSectionItems objectAtIndex:row]; 

    //setup my cells data here...

    return cell;
}

Is there anything wrong with this code?

has anyone seen anything like this before?

解决方案

Cells are supposed to be reused. If you want to turn that off, turn off cell reuse.

Your problem is actually in the code you didn't include.

//setup my cells data here...

This code is responsible for completely loading every aspect of the cell that varies between the rows in your table. That data that's showing up more than once? You need to set it in cases where you have it, or clear it if you don't.

For instance:

cell.textLabel.text = str ? str : @"";

In that way, the same few cells are used over and over again, and table cells don't need to be set up and destroyed frequently.

(As I mentioned, you can turn off cell reuse. But you should make this work.)

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

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