2011-06-15 34 views
0
@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回到保存的文本。问题是,它这样做时,它给我

- [CFString字符串_isNaturallyRTL]:消息发送到释放实例 0x2c459ff0

,当我看着self.text中的cellForRowAtIndexPath在调试器

,它说它是NSZombie_NSString ...所以不知何故它正在dealloc'd。我试图设置属性来复制,使用[initWithString]复制字符串...为什么字符串获取dealloc'd?

+0

你为什么要将self.text复制回textField.text?你的意思是indexPath.row == 1而不是== 1节吗? – Legolas 2011-06-15 14:58:25

+0

是的,我的意思是部分。我有2个部分,第零部分有7个字段由单独的数据支持,第一部分有1个字段,我用NSString支持。当视图被回收时,我需要能够将textField.text与我的支持数据重新分配。 – 2011-06-15 15:09:20

+0

如果您想要的是每个单元格都有一个文本字段,最好的做法是将您的表格视图单元格子类化,并让该单元格管理文本字段的生命周期。 – zonble 2011-06-15 16:07:17

回答

0

一朝被蛇咬十年怕井绳。除此之外,我已经犯了这个错误。

在我acctual代码我有

text = textField.text; //var declared in class 

但我的意思是

self.text = textField.text; //to use the property like in my example 
1

textField.delegate = self; 
tableView.dataSource = self; 
+0

+1,因为这个问题是不公平的,我的例子会起作用。 – 2011-06-15 15:27:37

+0

.............. lol。 – Legolas 2011-06-15 15:40:17

1

你是哪里的值设置为 “文本” 属性?

尝试将其设置为self.text = [NSString stringWithFormat:@"This is some text"];

话虽这么说,作为最佳做法,我强烈建议不使用这样的可以与原生对象的属性(UILabel.text,UITextField.text等混淆“文本”的名字)。它从长远来看有助于:)

+0

为保密起见,我通常在SO上更改我的标识符名称。但这是很好的建议! – 2011-06-15 20:18:07

+0

有趣的是你提到了,但我今天要说的是:) – Sid 2011-06-15 21:01:09