2011-06-15 51 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