没有从UITableView Cell获得第一个UITextField的值

时间:2016-02-29 17:51:38

标签: ios uitextfield uitableview

在TagViewController.m中我将标签添加到cellview.xib的文本字段

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    TagCell *cell = (TagCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TagCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.txtCellTag.tag = indexPath.row;
    return cell;
}

开启按钮点击我试图通过以下功能

获取textField的值
(IBAction)btnSave:(id)sender {

    for(int i=0;i<[dataId count];i++) {

        UITableViewCell *cell = [[self tblImages] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        if(cell == nil) {
            NSLog(@";Cell is nil"); // Cell is always nil
        } else {
            UITextField *textField = (UITextField *)[cell viewWithTag:i];
            NSLog(@"Value is %@", textField.text);
        }
    }
}

我正在获得All Textfield EXCEPT First Row的价值 第一个字段值始终为NULL

1 个答案:

答案 0 :(得分:0)

最好对UITableViewCell进行类型转换并从单元格中获取文本字段。

for(int i=0;i<[dataId count];i++) {

    UITableViewCell *cell = (TagCell *)[[self tblImages] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    if(cell == nil) {
        NSLog(@";Cell is nil"); // Cell is always nil
    } else {
        UITextField *textField = cell.txtCellTag;
        NSLog(@"Value is %@", textField.text);
    }
}