Iphone 0索引处的图像超过

Iphone 0索引处的图像超过,iphone,objective-c,Iphone,Objective C,我有个棘手的问题。我的类是UITableViewController的子类,它有一个调用 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 调用上述内容的方法是: - (void) insertInfo:(UIImage *)image { self.hotelImage = nil; self.hotelImage = image; num

我有个棘手的问题。我的类是UITableViewController的子类,它有一个调用

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
调用上述内容的方法是:

- (void) insertInfo:(UIImage *)image
{
self.hotelImage = nil;
self.hotelImage = image;
numRows = numRows + 1;
[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:0]  withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
这就是我创建单元格的方式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
{
self.datacell = nil;
self.datacell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1   reuseIdentifier:@"SimpleTableIdentifier"] autorelease];
self.myImageView = nil;
self.myImageView = [[UIImageView alloc] initWithImage:self.hotelImage];
[self.myImageView setFrame:CGRectMake(5, 5, 80, 75)];
[self.datacell.contentView addSubview:self.myImageView];
[self.myImageView release];
self.tableView.userInteractionEnabled = YES;
self.tableView.separatorColor = [UIColor clearColor];
return self.datacell;
}
因此,我的问题是,当我滚动表格时,表格中的所有图像都会被索引为0的单个图像替换。我的猜测是,这与创建单元格时,节中的每个图像都被视为索引0有关,但创建表时会显示不同的图像。但是当用户滚动表格时,不同的图像会被第一个单元格的索引0中的图像取代

这正是发生的情况,当表格开始滚动时,第一个单元格上的图像显示在所有单元格上

我只是不知道当表格滚动时,如何使每个单元格保留其唯一的图像。我猜这与将图像放置在XPath.section中有关???但我不确定。有人能帮我解决这个问题吗

谢谢,,
Victor。

实际上,您是在使用insertInfo编辑表的内容,而不是编辑其实际来源,因此一旦需要重新绘制这些单元格,它们就会恢复到原始来源。尝试使用以下内容:

self.myImageView=[[UIImageView alloc] initWithImage:[myImageArray objectAtIndex:indexPath.row]];

事实上,tableView并不能让您的单元格保持不变。一旦您将它们从视图中滚动出来,它们就会消失、释放、解除分配(据您所知),并且表视图会向数据源请求新的替换单元。cellForRowAtIndexPath始终返回相同的单元格,与表视图的要求无关

您应该将发送给insertInfo的图像存储到一个数组中,然后在cellForRowAtIndexPath中,始终按索引从数组中返回图像,并通过indexPath.row和indexPath.section进行查找。每次滚动时,都会调用cellForRowAtIndexPath以获取屏幕上未显示的所有新单元格,这就是为什么单元格总是“更改为”发送到insertInfo的最新图像的原因

尝试向项目中添加一个新文件,即UITableViewController的子类,并检查其中的默认代码。您会发现dequeueReusableCellWithIdentifier也是一个有用的工具


这里的关键概念是cellForRowAtIndexPath:是一个问题,您的代码需要检查indexPath.section和indexPath.row的值,并返回属于该行/节的值。

Hi。谢谢你的留言。现在有了一些进展,但仍然没有100%的发挥作用。如果我这样做:self.myImageView=[[UIImageView alloc]initWithImage:[imagesArray objectAtIndex:indexPath.section]];并开始滚动,然后图像对于每个单元格都是唯一的,因为indexPath.section的值发生了变化(我使用NSLog查看了变化),但是在绘制单元格时,indexPath.section和indexPath.row的值都是0。所以现在它是向后的,当绘制单元格时,索引0处显示相同的图像,但当滚动时,图像是唯一的。还有其他建议吗?好的,我可以通过使用BOOL is_滚动来解决问题。因此,首先在is_scrolling为false时绘制单元格,然后在滚动表时使用NSMutableArray获取图像:if(is_scrolling){self.myImageView=[[UIImageView alloc]initWithImage:[imagesArray objectAtIndex:indexath.section];}否则{self.myImageView=[[UIImageView alloc]initWithImage:[imagesArray objectAtIndex:numRows-1];}有更优雅的解决方案吗???