UITableViewCell 中的 UITextField 插入后如何成为第一响应者?

6
正如标题所述,我在UITableViewCell内部有一个UITextField。我的tableView列出了一个名为tallies的数组中的项目:[String]。我在按下按钮时插入新条目并以新单元格动画显示。我希望在rowAnimation完成后,titleTextField成为第一响应者,但是到目前为止我无法让它起作用。新行完美地执行动画,但titleTextField不会成为第一响应者。这是我尝试过的方法:
CATransaction.begin()
CATransaction.setCompletionBlock {
    let cell = self.tableView(self.tableView, cellForRowAtIndexPath: self.editingIndexPath!) as! TallyCell
    cell.titleTextField.hidden = false
    cell.titleTextField.becomeFirstResponder()
}

tableView.beginUpdates()
tallies.append("Example")
tableView.insertRowsAtIndexPaths([editingIndexPath!], withRowAnimation: .Left)
tableView.endUpdates()

CATransaction.commit()
2个回答

0
在插入行后使textField成为第一响应者。

抱歉,我应该说明一下,如果我直接调用它,似乎会停止行动画。 - Leon
移除CTTransaction并使用以下方式进行操作: [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [cell.textLabel becomeFirstResponder]; - jagdish
1
也尝试过了,没有运气。 - Leon

0

试试这段代码

CATransaction.begin()
CATransaction.setCompletionBlock {
    let cell = self.tableView(self.tableView, cellForRowAtIndexPath: self.editingIndexPath!) as! TallyCell
    cell.titleTextField.hidden = false
}

tableView.beginUpdates()
tallies.append("Example")
tableView.insertRowsAtIndexPaths([editingIndexPath!], withRowAnimation: .Left)
tableView.endUpdates()
CATransaction.commit()
let cellNew = self.tableView(self.tableView, cellForRowAtIndexPath:self.editingIndexPath!) as! TallyCell
cellNew.titleTextField.becomeFirstResponder()

这是行不通的,因为 cell 是在块中定义的。 - Leon
但是你需要在CATransaction.commit()之后执行它。 - Pranav
请提供正确的文本以便我进行翻译。 - Pranav
没有运气。只是忽略了becomeFirstResponder的调用。 - Leon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接