【问题标题】:UITextField in UITableViewCell becomeFirstResponder after insertion [closed]UITableViewCell 中的 UITextField 在插入后成为FirstResponder [关闭]
【发布时间】:2016-11-23 12:13:38
【问题描述】:

正如标题所述,我在UITableViewCell 中有一个UITextField

我的 tableView 列出了一个名为 tallies: [String] 的数组中的项目

我在点击按钮时插入一个新条目,并在新单元格中制作动画。一旦rowAnimation 完成,我想要titleTextFieldbecomeFirstResponder,但到目前为止还不能让它工作。

新行动画完美,但 titleTextField 不会 becomeFirstResponder

这是我尝试过的:

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()

【问题讨论】:

    标签: ios swift uitableview uitextfield becomefirstresponder


    【解决方案1】:

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

    【讨论】:

    • 对不起,我应该说如果我直接调用它似乎会停止行动画
    • 删除 CTTransaction 并执行类似 [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [cell.textLabel becomeFirstResponder];
    • 也试过了。没有运气
    【解决方案2】:

    试试这个代码

    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 是在块中定义的
    • 但需要在 CATransaction.commit() 之后进行
    • 检查编辑的答案
    • 运气不好。简单地忽略 becomeFirstResponder 调用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多