2012-09-01 78 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; 
}