当我调用[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分配有关。我怎样才能解决这个问题?我怎样才能改变文字?
答案 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;