开发者

Image at 0 index overtake

I've got this nasty problem. My class, which is subclassing UITableViewController, has a method which is invoking

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

The method which is invoking what is above is this one:

- (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];
}

This is how I am creating a cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath 
{
self.datacell = nil;
self.datacell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1   reuseIdentifier:@"SimpleTableIdentifier"] autore开发者_如何转开发lease];
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;
}

So, my problem is that when I scroll the table, all images in the table get replaced by a single image at index 0. My guess is that it has something to do with the fact that when cell is created, every image in the section is considered at index 0, but different image is shown when table is created. But when the table is scrolled by user, different images get overtaken by an image the is in the index 0 of the first cell.

This is exactly what happens, the image on the first cell is shown on all cells when the table begins to scroll.

I just don't know how to make every cell retain its unique image when the table is scrolled. My guess is it has something to do with placing image indexPath.section??? But I am not sure. Could somebody please help me with this problem?

Thank you, Victor.


Effectively you're editing the contents of your table with insertInfo, but not editing its actual source, so as soon as it needs to redraw those cells, they get reverted to its original source. Try using something along the lines of:

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


The fact is that the tableView doesn't keep your cells around. As soon as you scroll them out of view they are gone, released, deallocated as far as you know, and the table view asks the data soucre for new replacement cells. Your cellForRowAtIndexPath is always returning the same cell, independent of what the table view asks for.

You should be storing the images sent to insertInfo into an array, then in cellForRowAtIndexPath, always return the image from the array by index, looked up by indexPath.row and indexPath.section. Every time you scroll, that cellForRowAtIndexPath is called to get all the new cells that weren't on screen, this is why the cells always "change to" the most recent image sent to insertInfo.

Try adding a new file to your project, subclass of UITableViewController, and check out the default code there. You'll find dequeueReusableCellWithIdentifier is also a useful one.

The key concept here is that cellForRowAtIndexPath: is a question, and your code needs to check the values of indexPath.section and indexPath.row, and return a value that belongs to that row/section.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜