如何保存tableView自定义单元格到可变数组



我有一个带有自定义单元格的tableView。Tableview是空的。我有"+"按钮添加我的自定义单元格标签和文本域

问题:我如何保存到可变数组textfield。当用户按下"+"按钮时,所有tableview单元格的文本

这里的"+"按钮代码…

- (IBAction)addButtonPress:(UIBarButtonItem *)sender {
    MYCustomTableViewCell *nextCell =[self.myTableViewProperty dequeueReusableCellWithIdentifier:cellId];
    if (!nextCell) {
        nextCell = [[MYCustomTableViewCell alloc] init];
    }
    MYCustomTableViewCell *previousCell =[self.myTableViewProperty dequeueReusableCellWithIdentifier:cellId];
    NSIndexPath *saveTextIndex = [NSIndexPath indexPathForItem:myCustomCellCount inSection:1];
    // -- i can't get my customCell.textField.text
    previousCell = [self.createTableView cellForRowAtIndexPath:saveTextIndex];
    NSLog(@"%@", previousCell.textField.text); ////- its null=(
    [cellArray addObject:nextCell];
    [self.myTableViewProperty reloadData];
}

添加标签属性到你的textField等于你的indexPath,然后你可以简单地通过他们的标签属性访问你的文本字段。

的例子:创建新的customCell

cell.textField.tag = indexPath.row;

获取cell的textField

MYCustomTableViewCell *previousCell =[self.myTableViewProperty cellForRowAtIndexPath:indexPath];
UITextField *txtF = (UITextField*)[previousCell viewWithTag:myCustomCellCount];

请在开始之前查看文档UITableViewDataSource Protocol Reference。这个链接可能也很有用。UITableView只是用来显示你的模型(称为数据源)的内容。因此,当addButtonPress:被调用时,你应该插入一个数据对象到你的数据源(例如一个数组),并告诉表视图重新加载它的内容。在几乎所有情况下,dequeueReusableCellWithIdentifier:只在tableView: cellForRowAtIndexPath:中被调用。这就是创建表示数据源数据对象的新表视图单元格的地方。不需要将表格视图的单元格存储在数组中因为这是由表格视图自己完成的。当用户按下按钮后,你只需遍历数据源数组并读取它的数据对象。

最新更新