views:

50

answers:

2

Is there any way to change something about a table cell on the iPhone based on the previous or next table cell before that cell is displayed? This might be confusing, so here's an example:

Let's say I have a table like this (I'm just going to fill it with random values, so don't worry about the text contained in the cells):

*Cat
*Rabbit
*Dog
*Mouse

and there was a imageView.image attached to some cells (different images for different animals and you don't know which ones might have images) and at some point (programmatically) new animals are entered into the list sandwiching the animals that are already entered like so:

*[animal]
*Cat
*[animal]
*Rabbit
*[animal]
*Dog
*[animal]
*Mouse
*[animal]

How would I go about moving the images associated with the previously listed animals?

Hopefully you get a kick out of how ridiculous this question sounds, but I swear it is a serious question that is driving me insane and I didn't know how else to describe it.

A: 

I hope I've got the right end of the stick here, but: In the cellForRowAtIndexPath: method, you could get a pointer to the previous cell (and so any associated images, text, etc.):

    NSIndexPath *indexPathForPreviousCell=[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];
    UITableViewCell *previousCell=[tableView cellForRowAtIndexPath:indexPathForPreviousCell];

I hope this is what you needed...

Felixs
When I try that, I get (null) no matter what as a response for previousCell
wierddemon
A: 

Felixs' answer probably works, but it sounds like you are thinking of it from view-manipulation angle, and not a model-manipulation angle. It sounds like you really have two parallel models: the list of animals and the set of images. Why not just reloadData when either thing changes and rebuild the cells appropriately from inside your controller?

Reaching into existing cells based on their index feels fragile to me.

quixoto
I'm not sure I follow this :/
wierddemon
quixito is right-my answer is a somewhat 'quick fix' way of answering your question. You should rather change your data, because you also might need to access other attributes based on it (such as row height).
Felixs
What's the best way to associate the imageView.image with the item so that I can rebuild it that way?
wierddemon