引用具有重用标识符的单元

时间:2016-07-28 19:28:30

标签: ios objective-c uitableview reference reuseidentifier

我正在开发iOS-App,因此我使用了UITableViewController。在" cellForRowAtIndexPath"我使用具有重用标识符的单元格:

[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle1 reuseIdentifier:textFieldIdentifier];

问题是一些细胞彼此依赖,例如如果用户在一个单元格中输入文本,则另一个单元格会更改其值。

那么保护对必须更改的单元格的引用的最佳方法是什么?问题是,如果我在" textFieldDidChange"的回调期间保护" cellForRowAtIndexPath"中的引用?引用可能会被破坏,例如如果单元格不可见或其他单元格由于重用标识符而具有地址?!

2 个答案:

答案 0 :(得分:1)

不要尝试保存对缓存单元格的引用。更新显示表格数据源中所需的内容,然后调用reloadData。这样,该表负责刷新可见单元格并处理缓存...所以你不需要。

答案 1 :(得分:0)

我会为细胞制定一个协议

实施例

@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
相关问题