0

我有一个填充了可变数组的表。单元格是自定义的,带有 UITextView,委托给 customcellclass,我想保留该值。每个表 reloadData 后它都会被删除。

关于如何保持它的任何想法?我有但无法实现的想法:

  • 将 textView 值存储在填充表的同一数组中。从哪里?TextViewDidEnd 编辑?如何传递值,以及如何知道它是哪一行?
  • 保持一个 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;

}

4

2 回答 2

2

设置cell.textLabel.delegateself(your UIViewController),然后在textFieldDidEndEditing:方法中,可以这样检索 所在单元格对应的 indexPath UITextField

-(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以恢复先前键入的文本。

不要忘记在您的或类似的地方的方法中实例化您的NSMutableDictionary( self.mutableDictionaryOfTextValues = [NSMutableDictionary dictionary];) 。initUIViewController


请注意,这仅在您结束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;
}
于 2012-09-01T18:55:39.260 回答
0

此解决方案假设您为文本字段分配了标签,您可以在 cellForRowAtIndexPath 方法中声明标签:

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

您可以在 textFieldDidEndEditing 方法中获取您的值:

-(void)textFieldDidEndEditing:(UITextField *)textField{

if ([textField.text isEqualToString:@""]) {
    textFieldContent[textField.tag] =@"";

}
textFieldContent[textField.tag] = textField.text;
}

然后在 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;
}
于 2014-11-28T22:01:55.923 回答