@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