我目前正在尝试创建一个类似于苹果提醒的to应用程序。现在,当我通过单击UIButton添加一个新任务时,将创建一个包含UITextfield的新单元,并将其指定为第一个响应程序。但是,当我这样做时,键盘就会像预期的那样显示出来,但是没有光标,我也不能编辑。当我再次单击textfield时,我可以按预期进行编辑。
任何帮助都将不胜感激。
@IBAction func addTask(sender: UIBarButtonItem)
{
// Add a new task
let newIndexPath = NSIndexPath(forRow: tasks.count, inSection: 0)
let task = Task(name: "")!
tasks.append(task)
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
let cellIdentifier = "TaskTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: newIndexPath) as! TaskTableViewCell
let db_Task = PFObject(className: "Task")
db_Task["TaskName"] = ""
db_Task["Completed"] = false
db_Task["group"] = getGroupId()
db_Task.saveInBackgroundWithBlock
{(success: Bool, error: NSError?) -> Void in
print("Object has been saved.")
}
cell.nameLabel.becomeFirstResponder()
}发布于 2016-01-03 23:45:25
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
let cellIdentifier = "TaskTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: newIndexPath) as! TaskTableViewCell不能通过“dequeue”方法获得插入的单元格。永远不要从cellForIndexPath中调用此方法。插入后应该这样做:
let cell = tableView.cellForIndexPath(indexPath)发布于 2016-01-04 03:41:01
你可以试试
self.view.endEditing发布于 2016-01-04 04:29:44
应该对要聚焦的对象调用BecomeFirstResponder。在这种情况下,cell.nameLabel是错误的对象。
你应该打电话给cell.[your_text_field]。
https://stackoverflow.com/questions/34582790
复制相似问题