Reputation: 419
I'm developing an iOS-App and therefore I use a UITableViewController. Within "cellForRowAtIndexPath" I use cells with reuse identifiers:
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle1 reuseIdentifier:textFieldIdentifier];
The problem is that some cells have a dependecy on each other, e.g. if the user enters text in one cell another cell changes its value.
So what is the best way to safe a reference to the cell that has to be changed? The problem is, that if I safe the reference within "cellForRowAtIndexPath", during the callback for "textFieldDidChange" the reference might be broken, e.g. if the cell is not visible or another cell has the adress due to the reuse identifier?!
Upvotes: 0
Views: 223
Reputation: 145
I would make an protocol for the cells
Example
@protocol MyProtocol <NSobject>
- (void) changeText:(NSString)theText;
@end
@interface TableViewCell1 : UITableViewCell
@property (nonatomic, weak) id<MyProtocol> delegate;
@end
@implementation TableViewCell1
//put this in the method where you get the value of the textfield
[self.delegate chageText:@"Hello"];
@end
@interface TableViewCell2 : UITableViewCell <MyProtocol>
@end
@implementation TableViewCell2
- (void) chageText:(NSString *)text {
self.textLabel.text = text;
}
@end
Upvotes: 0
Reputation: 31016
Don't try to save references to cached cells. Update whatever you need to display in the table's data source and then call reloadData
. That way, the table takes care of refreshing visible cells and dealing with the cache...so you don't need to.
Upvotes: 1