【问题标题】:Update UITextFields from UITableViewCells without reloadind data从 UITableViewCells 更新 UITextFields 而无需重新加载数据
【发布时间】:2012-06-22 14:09:00
【问题描述】:

我有一个带有自定义UITableViewCellsUITableView,每个都有一个UITextField。我为每个 textField 分配一个带有值的标签:indexPath.row + 100

好吧,当我在特定的文本字段中键入内容时,我想更新每个单元格的每个文本字段。更清楚地说,当我输入一个数字时,我的视图控制器应该进行一些计算,然后将结果分配给所有其他 textFields,并且每次从 textField 修改文本时都必须这样做,假设我输入了 1(进行一些计算和将结果赋值给 textFields) ,然后我输入 2 ,现在要计算的数字将是 12 等等。

问题是我可以在不关闭键盘的情况下从 tableView 重新加载数据。系统会自动隐藏 UIKeyboard ,所以这种情况下 reloaddata 不起作用。

我尝试使用 NSMutableArray 来存储所有这些文本字段,但是当从 cellForRowAtIndexPath 添加它们时,它们会得到很多。

如何正确更新所有这些UITextFields

【问题讨论】:

    标签: ios5 uitableview ios4 uitextfield


    【解决方案1】:

    它只需要更新可见单元格,而不是全部。 假设内容计算公式很简单:

    -(NSString*) textForRowAtIndex:(int)rowIndex
    {
        return [NSString stringWithFormat:@"%d", startRowValue + rowIndex];
    }
    

    每个单元格都包含带有标签indexPath.row + 100UITextField对象:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString* cellId = @"cellId";
        UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if(!cell)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UITextField* tf = [[[UITextField alloc] initWithFrame:CGRectMake(10, 8, 280, 30)] autorelease];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) 
                                                         name:UITextFieldTextDidChangeNotification object:tf];
            tf.delegate = (id)self;
            [cell.contentView addSubview:tf];
        }
    
        UITextField* tf = (UITextField*)[[cell.contentView subviews] lastObject];
        tf.tag = indexPath.row + 100;
        tf.text = [self textForRowAtIndex:indexPath.row];
    
        return cell;
    }
    

    那么所有可见单元格都将在textFieldTextDidChange:方法中更新:

    -(void) textFieldTextDidChange:(NSNotification*)notification
    {
        UITextField* editedTextField = (UITextField*)[notification object]; 
        int editedRowIndex = editedTextField.tag - 100;
        int editedValue = [editedTextField.text intValue];
        startRowValue = editedValue - editedRowIndex;
    
        for (NSIndexPath* indexPath in [self.tableView indexPathsForVisibleRows])
        {
            if(indexPath.row != editedRowIndex)
            {
                UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
                UITextField* textField = (UITextField*)[cell.contentView viewWithTag:indexPath.row+100];
                textField.text = [self textForRowAtIndex:indexPath.row];
            }
        }
    }
    

    让我们有 50 个单元格:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 50;
    }
    

    完成编辑后隐藏键盘:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
    

    享受吧!

    【讨论】:

      猜你喜欢
      • 2012-07-25
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2014-08-08
      • 2018-08-09
      • 1970-01-01
      • 2016-01-12
      • 2017-04-19
      相关资源
      最近更新 更多