【发布时间】:2015-08-10 01:46:08
【问题描述】:
我有一个 UITableView 列表。我添加了代码,因此当点击列表项/行时,用户可以在模式视图中编辑单元格内容:
- 点击后,代码会将单元格的标题/描述保存到 vars
- 然后启动我的模态编辑器视图
- 主 VC 向编辑器 VC 传递标题/描述变量
- var 值被加载到文本框中,以便用户进行编辑
问题:
这是一种奇怪的行为。当我测试应用程序时,第一次在打开应用程序后点击一个单元格时,有时会延迟 2-3 秒,然后才会出现模态视图。我还注意到延迟发生在我第一次点击每个单元格时,但不是第二次。最后,有时在打开应用程序后,它似乎忽略了第一次点击。这是正常的核心数据获取请求行为吗?
在主 VC ViewDidLoad 上加载的代码
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Break
//Load the list from Core Data
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext!
let fetchRequest = NSFetchRequest(entityName:"TodayTask")
var error: NSError?
let fetchedResults = managedContext.executeFetchRequest(fetchRequest, error: &error) as? [NSManagedObject]
if let results = fetchedResults {
todayTaskList = results
} else {
println("Could not fetch \(error), \(error!.userInfo)")
}
//Break
//This provides a variable height for each row
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80.0
//Break
//Part of code for cell drag and drop functionality
let longpress = UILongPressGestureRecognizer(target: self, action: "longPressGestureRecognized:")
tableView.addGestureRecognizer(longpress)
}
在单元格点击时运行代码
//Action: Performs 'tap on edit' segue
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
passingEdit = true
performSegueWithIdentifier("modalToEditor", sender: nil)
}
//Prepares variables to be passed for editing
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "modalToEditor") && passingEdit == true {
//Assign selection to a variable 'currentCell'
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;
//Set cell text into variables to pass to editor
var cellNameForEdit = currentCell.nameLabel!.text
var cellDescForEdit = currentCell.descLabel.text
//Pass values to EditorView
var editorVC = segue.destinationViewController as! EditorView;
editorVC.namePassed = cellNameForEdit
editorVC.descPassed = cellDescForEdit
editorVC.indexOfTap = indexPath
}
}
【问题讨论】:
-
你应该在'BackGround'线程上调用
CoreData。 -
你在 iOS8 上吗?我认为这是一个错误。有关解决方法,请参阅this answer。下面评论中的链接也很有趣。
-
@pbasdf 我正在使用 8.4 - 我会尝试解决这个问题。非常感谢
标签: ios swift uitableview core-data modalviewcontroller