iphone:uitableview:单元格的内容在滚动时发生更改 [英] iphone : uitableview : contents of a cell change on scrolling

查看:65
本文介绍了iphone:uitableview:单元格的内容在滚动时发生更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,用于在表格视图中提供自定义单元格的内容我不知道出什么问题了,但是当我滚动一个表格视图时,conocants oc单元会动态变化

I have an array which I am using to furnish the contents of custom cell in table view I dont know whats wrong but when I scroll a tableview the contants oc cell changes dynamically

请帮助我解决此问题

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

static NSString *MyIdentifier = @"MyIdentifier";
MyIdentifier = @"tblCellView";
NSString *offendersImagePath = [self applicationDocumentsDirectory];
//NSLog(@"%@", dbPath);
offendersImagePath=[offendersImagePath stringByAppendingPathComponent:@"Images"];


CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = aCustomCell;
    aCustomCell=nil;
}

NSMutableArray *tempArray;//=[[NSMutableDictionary alloc] init];

tempArray=[offendersNamesList objectAtIndex:indexPath.row];
//NSLog(@"%d",indexPath.row);

offendersImagePath=[offendersImagePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",[tempArray objectAtIndex:0]]];
NSLog(offendersImagePath);
[[cell offendersImageView] setImage:[UIImage imageWithContentsOfFile:offendersImagePath]];
[cell.offendersNameLbl setText:[tempArray objectAtIndex:1]];
[cell.offendersViolation setText:[tempArray objectAtIndex:2]];
//[tempDictionary release];

//[cell setLabelText:[arryData objectAtIndex:indexPath.row]];
return cell;

}

推荐答案

这类问题中的典型问题是,您没有在cellForRowAtIndexPath中正确设置单元格中的内容.当某个单元格从屏幕上滚动下来时,系统会将其转储到回收队列"中(我的说法).当新单元格滚动到屏幕上时,系统会在此回收队列中查找可重复使用的单元格.
(((CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];)
如果找不到,它会继续并从头开始构建一个全新的.就您而言,无论出于何种原因,您似乎都无法正确设置单元格内容,并且看到的更改是未使用正确内容更新的回收单元.

What is typically the problem in these kinds of problems is that you are not setting up the content in your cell correctly in the cellForRowAtIndexPath. When a cell scrolls off the screen the system dumps it into a "Recycle Queue" (my term). As new cells scroll onto the screen the system looks in this recycle queue for cells it can reuse.
((CustomCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];)
If it does not find one it goes ahead and builds an entirely new one from scratch. In your case, it looks like for whatever reason you are not setting up the cell content correctly and the changes you are seeing are recycled cells that have not been updated with the correct content.

我不确定您出了什么问题,但是用于新单元格的代码有些奇怪.它应该看起来像这样:

I'm not sure exactly where you are going wrong but the code you are using for new cells is a little strange. It should look more like this:

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

这篇关于iphone:uitableview:单元格的内容在滚动时发生更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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