2012-09-01 16 views
0

我有一個表格填充了一個可變數組。這些單元格是自定義的,具有在customcellclass上委託的UITextView,我希望保留這個值。它在每個表reloadData之後被擦除。維護自定義單元格中的UITextView的值

有關如何保持它的任何想法?我有這個想法,但不能去:

  • 將textView值存儲在填充表的同一個數組上。來自哪裏? TextViewDidEndEditing?如何傳遞價值,以及如何知道它是哪一行?
  • 維護一個與另一個不同的textViews數組。溝通細胞表的同樣的問題,然後我也必須維護這個數組。另外,我應該將textView從.nib中移開。 ...我不知道更多的知道。

任何方法或幫助將不勝感激。謝謝! PS:我做了大量的搜索,大部分指向表單,但這是一個可變數組,我將在表上有未定義的行。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *CellIdentifier = @"Cell"; 


GuiCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) { 
    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"GuiCustomCell" owner:nil options:nil]; 

    for (UIView *view in views) { 
     if([view isKindOfClass:[UITableViewCell class]]) 
     { 
      cell = (GuiCustomCell*)view; 
     } 
    } 
} 

if (timesArray || [timesArray count]) {   

    TakenTime *time = [[TakenTime alloc] init]; 
    time = [timesArray objectAtIndex:indexPath.row]; 

    if ([timeFormat isEqualToString:@"seconds"]) { 
     cell.detailTextLabel.text = [time stringTimeFormat2]; 
    } else { 
     cell.detailTextLabel.text = [time stringTimeFormat1]; 
    } 

    cell.detailTextLabel.font = [UIFont systemFontOfSize:25]; 
    cell.textLabel.font = [UIFont systemFontOfSize:18]; 
    cell.textLabel.textAlignment = UITextAlignmentLeft; 
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 
    cell.textLabel.text = [NSString stringWithFormat:@"%i.",indexPath.row+1];   

}else{ 

    cell.textLabel.text = @" "; 

} 

[cell setDefaultText:TRUE]; 

return cell; 

}

+0

對我們的任何代碼,看看?就像-cellForRowAtIndexPath:首先。 –

+0

你在編輯中去了!任何你需要的!謝謝 – g10k

回答

2

設置cell.textLabel.delegateself(您UIViewController),然後在textFieldDidEndEditing:方法,您可以檢索對應於該UITextField位於這樣的細胞的indexPath:

-(void)textFieldDidEndEditing:(UITextField*)aTextField 
{ 
    UITableViewCell* containerCell = (UITableViewCell*)aTextField.superview; 
    NSIndexPath* indexPath = [self.tableView indexPathForCell:containerCell]; 
    // Store the text, for example in an NSMutableDictionary using the indexPath as a key 
    [self.mutableDictionaryOfTextValues setValue:aTextField.text forKey:indexPath]; 
} 

當然,在您的tableView:cellForRowAtIndexPath:方法中,您將檢索存儲在[self.mutableDictionaryOfTextValues objectAtIndex:indexPath]中的文本值並將其影響到cell.textLabel.text恢復先前輸入的文本。

不要忘了你的實例化在你的UIViewController或一些地方類似的init方法NSMutableDictionaryself.mutableDictionaryOfTextValues = [NSMutableDictionary dictionary];)。


請注意,如果您滾動前結束UITextField的版本,這隻會工作,那就是當你的UITextField收到resignFirstResponder(例如,如果您關閉鍵盤,或者用戶點擊在另一個UITextField,將成爲新的firstResponder),因爲這是當調用textFieldDidEndEditing:委託方法時。 如果你想每次用戶輸入一個字符或修改文本存儲文本,而無需等待用戶去另一個文本字段和textFieldDidEndEditing:被解僱,你可以使用textField:shouldChangeCharactersInRange:replacementString:委託方法代替:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
    // Compute the new text that we will obtain once the new character has been validated 
    NSString* newText = [textField.text replaceCharactersInRange:range withString:string]; 
    // Store it in our dictionary. Same as before, but use newText instead of textField.text 
    UITableViewCell* containerCell = (UITableViewCell*)aTextField.superview; 
    NSIndexPath* indexPath = [self.tableView indexPathForCell:containerCell]; 
    // Store the text, for example in an NSMutableDictionary using the indexPath as a key 
    [self.mutableDictionaryOfTextValues setValue:newText forKey:indexPath]; 
    // Accept the new character 
    return YES; 
} 
+0

一切似乎都工作得很好,但是... indexPath每次都會返回相同的行/段,因此它始終將文本存儲在相同的位置。 – g10k

+0

'containerCell'是否也一樣?和'UITextField'?您是否嘗試使用調試器進行挖掘? – AliSoftware

+0

爲愚蠢的問題辯解,但如何知道containerCell是否總是相同的實例? UITableViewCell的斷點* containerCell =(UITableViewCell *)textView.superview; 然後看看哪裏? – g10k

0

該解決方案是假設你指定標籤的文本框,您可以cellForRowAtIndexPath方法中聲明標籤:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath { 
//Code 
// Configure the cell... 
cell.myTextbox.tag = indexPath.row; 
} 

您在textFieldDidEndEditing方法讓你的價值觀

然後你設置你的cellForRowAtIndexPath方法 文本:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath { 
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Mycell" forIndexPath:indexPath]; 


// Configure the cell... 
cell.myLabel.text = [nombres objectAtIndex:indexPath.row]; 
cell.myTextField.tag = indexPath.row; 
if([textFieldContent count] < indexPath.row){ 
    cell.myTextField.text = @""; 

} 
else{ 
if ([textFieldContent count] == 0) { 
    cell.myTextbox.text = @""; 

} 
else{ 
    cell.myTextbox.text = [textFieldContent objectAtIndex:indexPath.row]; 
} 

} 

return cell; 
}