UITableView单元格中的UITextField在删除数据后无法成为FirstResponder
“我的表视图”具有内联单元格 我在cellForRowAtIndexPath中找到了:UITableView单元格中的UITextField在删除数据后无法成为FirstResponder,uitableview,Uitableview,“我的表视图”具有内联单元格 我在cellForRowAtIndexPath中找到了: VMEditableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditableCell"]; cell.editableTF.font = [UIFont fontWithName:@"Helvetica" size:17.0]; cell.editableTF.delegate = sel
VMEditableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditableCell"];
cell.editableTF.font = [UIFont fontWithName:@"Helvetica" size:17.0];
cell.editableTF.delegate = self;
cell.editableTF.tag = indexPath.row;
[cell.editableTF setBorderStyle:UITextBorderStyleNone];
if ([self.extrasArray[indexPath.row] isEqual:@0]) { // recognise new added cell
self.extrasArray[indexPath.row] = @"";
[cell.editableTF becomeFirstResponder];
}
cell.editableTF.text = self.extrasArray[indexPath.row];
return cell;
当我启动表视图时,内联添加工作正常
当我使用“清除整个列表”按钮时,问题就开始了——它只是从extrasArray中删除所有对象
当我在清除后添加单元格时,它被不正确地退出队列,文本字段没有响应becomeFirstResponder。
(不是零,它调用textFieldShouldBeginEditing(总是是的),但没有更多的事情发生——没有键盘出现
我的最后一个想法是在单元实现内部重写prepareForReuse方法——但遗憾的是,将editableTF设置为nil和重新注入都不起作用
我怎样才能强迫细胞重新自我创造而不是应付?试着创建一个这样的细胞
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Set up the cell..
[self configureCell:cell atIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
“所以今天早上的代码解析非常好,最后我想出了一个解决这个问题的方法: 我去掉了这个单元格的故事板,并将自定义单元格的所有指针都改为strong(所以我拥有它们) Dequeque可重用单元正在从删除的行重新创建UITextField,这使控制器对所有权感到困惑,并成为第一响应者 请注意,在CellInit内部创建它们并没有起到作用 TextField必须创建并添加为子视图(也指定给强指针,以便我以后可以控制它) 下面是一些代码:
VMEditableTableViewCell *cell = [[VMEditableTableViewCell alloc] init];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UIImage *bulletImage = [UIImage imageNamed:@"common_ico_dot.png"];
UIImageView *bulletImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,17,10, 10)];
bulletImageView.image = bulletImage;
UIImage *tickImage = [UIImage imageNamed:@"common_green_tick.png"];
UIImageView *tickImageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.bounds.size.width-30, 17, tickImage.size.width, tickImage.size.height)];
tickImageView.image = tickImage;
tickImageView.hidden = YES;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(60, 10, 227, 30)];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;
textField.font = [UIFont fontWithName:@"Maven Pro" size:17.0];
textField.tag = indexPath.row;
[textField setBorderStyle:UITextBorderStyleNone];
[cell addSubview:bulletImageView];
cell.bulletImageView = bulletImageView;
[cell addSubview:tickImageView];
cell.tickImageView = tickImageView;
if (cell.editableTF == nil) {
[cell addSubview:textField];
cell.editableTF = textField;
}
尝试创建这样的单元格“静态NSString*CellIdentifier=@”cell;UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:nil];if(cell==nil){cell=[[UITableViewCellAlloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];//设置单元格..[self-configureCell:cell atIndexPath:indexPath];cell.selectionStyle=UITableViewCellSelectionStyleNone;}ye-但是CustomCell中的textField和imageView将为零。在init中实例化它们也不起作用…尝试在CustomCell外声明textField和imageView。它还创建空白单元格。(在Debugger中创建了它们-但它们不在单元格中出现)我使用故事板和iOS6,所以不需要这样做。尽管如此(并在initWithStyle中分配所有子视图)-创建空白单元格。