Modifying a NSMutableArray from a TableCell



我有一个TableView(名为myTableView),它包含ViewController内部的原型单元格(名为:TableCell)(不是TableViewController,名为:ViewController)。

每个单元格有4个不同的文本字段,显示来自不同NSMutableArrays的数据(在ViewController.h中声明,名称为:Legajos、Nombres、Cbus、Sueldos、Contribuciones),因此例如,第0行的单元格将显示Legajos和Nombres等中索引0处的对象。所有这些都在工作,我甚至可以毫无问题地添加和删除行。

现在是我的问题。我有文本字段(声明为TableCell.h)显示NSMutableArrays(在ViewController.h中声明)的文本,但我只能进行"单程旅行",也就是说,文本字段只显示NSMugableArrays的文本,但是我还需要它们来修改NSMutabableArray,这样我就可以保存该数组,然后加载它

NSMutableArrays声明:(ViewController.h)

@property (strong, nonatomic) NSMutableArray *Legajos;
@property (strong, nonatomic) NSMutableArray *Nombres;
@property (strong, nonatomic) NSMutableArray *Cbus;
@property (strong, nonatomic) NSMutableArray *Sueldos;
@property (strong, nonatomic) NSMutableArray *Contribuciones;

以下是单元格获取文本的方式:(ViewController.m)

cell.legajo.text = [Legajos objectAtIndex:indexPath.row];
cell.nombre.text = [Nombres objectAtIndex:indexPath.row];
cell.cbu.text = [Cbus objectAtIndex:indexPath.row];
cell.sueldo.text = [Sueldos objectAtIndex:indexPath.row];
cell.contribucion.text = [Contribuciones objectAtIndex:indexPath.row];

这是细胞的IBOutlets:(TableCells.h)

@property (strong, nonatomic) IBOutlet UILabel *legajo;
@property (strong, nonatomic) IBOutlet UITextField *nombre;
@property (strong, nonatomic) IBOutlet UITextField *cbu;
@property (strong, nonatomic) IBOutlet UITextField *sueldo;
@property (strong, nonatomic) IBOutlet UITextField *contribucion;

很抱歉,如果很难理解。谢谢你的帮助。

最简单的方法可能是将(void) textFieldDidEndEditing:(UITextField *)textField方法添加到UITextField委托中。您可以在创建时为每个UITextField分配一个标记,这样您就知道要编辑哪个数组,或者我相信NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]];可以做到这一点。

 (void) textFieldDidEndEditing:(UITextField *)textField {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; 
        switch(indexPath.section) 
    {
        case '0':
            // edit object in first array at index indexPath.row
            [firstArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '1':
            // edit object in second array at index indexPath.row
            [secondArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '2':
            // edit object in third array at index indexPath.row
            [thirdArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '3':
            // edit object in fourth array at index indexPath.row
            [fourthArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '0':
            // edit object in fifth array at index indexPath.row
            [fifthArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        default :
            }
    }

这是我打出来的一个快速例子。

最新更新