【发布时间】:2011-09-15 15:04:01
【问题描述】:
@property (nonatomic, retain) NSString *text;
使用此属性,我可以保存在 UITableViewCell 中创建的文本字段中的文本
static NSString *cellIdentifier = @"fieldTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 12, 275, 25)];
textField.clearsOnBeginEditing = NO;
textField.returnKeyType = UIReturnKeyDone;
[textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:textField];
}
// re-get textField and set tag to section
if (section == 1) {
textField.text = self.text; // Where the problem is
textField.tag = section;
}
和
- (void)textFieldDone:(id)sender {
UITextField *field = sender;
if (field != nil) {
NSInteger section = field.tag;
if (section == 1) {
self.text = field.text;
}
}
}
但是,在 cellForRowAtIndexPath 中将 textField.text 设置回保存的文本。问题是当它这样做时它给了我
-[CFString _isNaturallyRTL]:消息发送到释放的实例 0x2c459ff0
当我在调试器中查看 cellForRowAtIndexPath 中的 self.text 时,它说它是 NSZombie_NSString ...所以不知何故它正在被释放。我尝试将属性设置为复制,使用 [initWithString] 复制字符串...为什么字符串会被释放?
【问题讨论】:
-
为什么要将 self.text 复制回 textField.text ?你的意思是 indexPath.row ==1 而不是 section ==1 吗?
-
是的,我的意思是部分。我有 2 个部分,第 0 部分有 7 个由单独数据支持的字段,第 1 部分有 1 个字段,我用 NSString 支持。当视图被回收时,我需要能够使用我的支持数据重新分配 textField.text。
-
如果你想要的是每个单元格都有一个文本字段,更好的做法是将你的表格视图单元格子类化,并让单元格来管理文本字段的生命周期。
标签: iphone objective-c ios memory-management properties