简体   繁体   English

滚动后,表格视图单元格的内容发生更改

[英]Contents of table view cells change after scrolling

I am using a xib and created a UITableViewController. 我正在使用xib并创建了UITableViewController。 In my cellForRowAtIndex, I dequeue and set a custom table cell like this: 在我的cellForRowAtIndex中,我出队并设置一个自定义表格单元格,如下所示:

 CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
 cell.delegate = self;

then I set some conditions just to set up the cells. 然后我设置一些条件只是为了设置单元。 I did something like this: 我做了这样的事情:

 if (indexPath.row == 2 || indexPath.row == 5) {
    cell.addressLabel.hidden = YES;
 } else {
     cell.storeHourTitle.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:0];
     cell.addressLabel.hidden = NO;
 }

just to show more information, my heightForRowAtIndexPath has: 为了显示更多信息,我的heightForRowAtIndexPath具有:

  if (indexPath.row == 2 || indexPath.row == 5) {
        return 280;
  } else {
        return 520;
  }

I've also set: 我还设置了:

self.tableView.delegate = self
self.tableView.datasource = self

but I don't think it's necessary? 但我认为没有必要吗? since I'm using a UITableViewController, please verify this for me. 由于我使用的是UITableViewController,请为我进行验证。

So now when I run my app, the cells load properly, at least the first 5. Once I scroll down, the 5th row's content is incorrect and when I scroll back up, some of the contents change. 因此,现在当我运行我的应用程序时,单元格将正确加载,至少是前5个单元格正确加载。向下滚动时,第5行的内容不正确;当向上滚动时,某些内容会更改。 (eg the first cell's content becomes the last cell's (row = 6)) (例如,第一个单元格的内容成为最后一个单元格的内容(行= 6))

I only return 6 in my numberOfRowsInSection. 我在numberOfRowsInSection中仅返回6。

Am I not dequeueing the cell properly? 我不能正确地将细胞出队吗? From what I've learned, if I use 从我学到的东西,如果我使用

dequeueReusableCellWithIdentifier forIndexPath

I don't have to do a 我不必做

if (cell == nil) 

because it will always return something. 因为它将总是返回一些东西。 If you guys spotted any flaw in my UITableView logic, please point it out to me, Thanks! 如果你们发现我的UITableView逻辑中有任何缺陷,请向我指出,谢谢!

EDIT: 编辑:

More detailed code cellForRowAtIndexPath 更详细的代码cellForRowAtIndexPath

CustomStoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomStoreTableViewCell" forIndexPath:indexPath];

cell.delegate = self;
if (indexPath.row == 2 || indexPath.row == 6) {

    cell.operatingHoursLabel.hidden = YES;
    cell.operatingHoursLabel2.hidden = YES;
    cell.addressLabel.hidden = YES;
    cell.numberLabel.hidden = YES;

    cell.storeHourTitle.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:0];
    cell.storeHourTitle.textColor = [UIColor blackColor];
    cell.addressTitle.hidden = YES;
    cell.phoneTitle.hidden = YES;
    cell.emailTitle.hidden = YES;
    cell.emailLabel.hidden = YES;

}
else{

    cell.topImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[self.dict objectForKey:@"Picture"] objectAtIndex:indexPath.row]]];
    cell.operatingHoursLabel.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:0];
    cell.operatingHoursLabel2.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:1];

    cell.addressLabel.text = [[self.dict objectForKey:@"Address"] objectAtIndex:indexPath.row] ;
    cell.addressLabel.numberOfLines = 0;
    cell.numberLabel.text = [[self.dict objectForKey:@"Phone"] objectAtIndex:indexPath.row];
}
return cell;

First things first, if you are using UITableViewController you don't need to set data source and delegate. 首先,如果您使用的是UITableViewController ,则无需设置数据源和委托。 But if you do, nothing bad happens ;) 但是,如果您这样做,则不会发生任何不好的事情;)

If you are seeing weird things after scrolling tableView, there is absolutely something wrong with your cellForRowAtIndexPath: method. 如果滚动tableView后看到奇怪的事情,则cellForRowAtIndexPath:方法绝对存在问题。 In most cases you are setting something conditionally and forgot to set or reset cell for else condition. 在大多数情况下,您有条件地设置某些内容,而忘记为else条件设置或重置单元格。 Or, do you have a custom cell (A cell defined in xib) or have implemented a custom cell controller? 或者,您是否具有定制单元(在xib中定义的单元)或已实现定制单元控制器? In that case use prepareForReuse method to do cleanup work. 在这种情况下,请使用prepareForReuse方法进行清理工作。 (I'm guessing you are, because you are setting delegate for cells, meaning there is something going on in your cell) (我猜你是,因为您正在为单元格设置委托,这意味着您的单元格中正在发生某些事情)

But for the flaws you have been asking for, I just hope you have defined the delegate property of your cell as weak otherwise you'll get memory leaks. 但是对于您一直在要求的缺陷,我只是希望您已将单元的delegate属性定义为weak否则会导致内存泄漏。

After posting your cellForRowAtIndexPath it is clear for me now, you are setting some values in your if statement, but forgot to setting them to other values in your else statement. 在发布cellForRowAtIndexPath之后,现在对我来说很清楚,您正在if语句中设置一些值,但是忘记了在else语句中将它们设置为其他值。 you should always set values symmetrically for your dequeued cells, otherwise they keep their old values set haphazardly in past! 您应该始终为已出队的单元格对称地设置值,否则它们会过去随意设置旧值!

it could be something like this: 可能是这样的:

CustomStoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomStoreTableViewCell" forIndexPath:indexPath];

cell.delegate = self;
if (indexPath.row == 2 || indexPath.row == 6) {

    cell.operatingHoursLabel.hidden = YES;
    cell.operatingHoursLabel2.hidden = YES;
    cell.addressLabel.hidden = YES;
    cell.numberLabel.hidden = YES;

    cell.storeHourTitle.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:0];
    cell.storeHourTitle.textColor = [UIColor blackColor];
    cell.addressTitle.hidden = YES;
    cell.phoneTitle.hidden = YES;
    cell.emailTitle.hidden = YES;
    cell.emailLabel.hidden = YES;

}
else{
cell.operatingHoursLabel.hidden = NO;
    cell.operatingHoursLabel2.hidden = NO;
    cell.addressLabel.hidden = NO;
    cell.numberLabel.hidden = NO;
    cell.addressTitle.hidden = NO;
    cell.phoneTitle.hidden = NO;
    cell.emailTitle.hidden = NO;
    cell.emailLabel.hidden = NO;
    cell.topImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[self.dict objectForKey:@"Picture"] objectAtIndex:indexPath.row]]];
    cell.operatingHoursLabel.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:0];
    cell.operatingHoursLabel2.text = [[[self.dict objectForKey:@"Hours"] objectAtIndex:indexPath.row] objectAtIndex:1];

    cell.addressLabel.text = [[self.dict objectForKey:@"Address"] objectAtIndex:indexPath.row] ;
    cell.addressLabel.numberOfLines = 0;
    cell.numberLabel.text = [[self.dict objectForKey:@"Phone"] objectAtIndex:indexPath.row];
}
return cell;

Your code is like this 您的代码是这样的

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
 cell.delegate = self;

Change this code to like this 将此代码更改为这样

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];

You set delegate for table not cell. 您设置表的委托而不是单元格。

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

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