0

当我调用 [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;

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

4

1 回答 1

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;
于 2013-04-03T16:57:25.777 回答