@property (nonatomic, retain) NSString *text;
保存文本字段与此属性,我节省了从一个文本框的文字我在一个UITableViewCell尝试在TableView中
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回到保存的文本。问题是,它这样做时,它给我
,当我看着self.text中的cellForRowAtIndexPath在调试器- [CFString字符串_isNaturallyRTL]:消息发送到释放实例 0x2c459ff0
,它说它是NSZombie_NSString ...所以不知何故它正在dealloc'd。我试图设置属性来复制,使用[initWithString]复制字符串...为什么字符串获取dealloc'd?
你为什么要将self.text复制回textField.text?你的意思是indexPath.row == 1而不是== 1节吗? – Legolas 2011-06-15 14:58:25
是的,我的意思是部分。我有2个部分,第零部分有7个字段由单独的数据支持,第一部分有1个字段,我用NSString支持。当视图被回收时,我需要能够将textField.text与我的支持数据重新分配。 – 2011-06-15 15:09:20
如果您想要的是每个单元格都有一个文本字段,最好的做法是将您的表格视图单元格子类化,并让该单元格管理文本字段的生命周期。 – zonble 2011-06-15 16:07:17