如何在UITableViewCell内的文本字段中设置新值?

时间:2013-04-03 16:45:37

标签: iphone ios uitableview

当我调用[Self.table reloadData]

时,是否会触发此代码
if (cell == nil){...}

我想知道因为我在单元格中添加了一个UITextField:

//cellForRowAtIndexPath
static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        recepientTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+40, 10, 469, 20)];
        [cell addSubview:recepientTextField];
    }
    recepientTextField.text = recipient;
    return cell;

然后我从互联网上下载了一些东西,然后我想在TableView中的recepientTextField.text中显示信息。所以我调用为recipient变量设置一个新值,然后[self.tableView realoadData]但它崩溃了。我认为这与textfields分配有关。我怎样才能解决这个问题?我怎样才能改变文字?

1 个答案:

答案 0 :(得分:0)

当重新加载cell != nil案例时,recipientTextField具有空引用,因此崩溃

试试这个

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    recepientTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x+40, 10, 469, 20)];
    [recepientTextField setTag:333];
    [cell addSubview:recepientTextField];
}

UITextField *TF=(UITextField *)[cell viewWithTag:333];
TF.text = recipient;
return cell;