Ios 在tableview单元格中翻转文本
刷新tableview时,我遇到了一个特殊的问题。(我使用的是Ios 在tableview单元格中翻转文本,ios,iphone,swift,uitableview,uirefreshcontrol,Ios,Iphone,Swift,Uitableview,Uirefreshcontrol,刷新tableview时,我遇到了一个特殊的问题。(我使用的是UIRefreshControl)如果我要减慢mo的速度,那么: 假设有3个可见单元格,A、B和C,按从上到下的顺序排列 1) 我向下拉,桌面视图显示它令人耳目一新 2) A获取应该放在B中的文本。B获取应该放在C中的文本。C在屏幕外 3) “刷新”结束后,表格将捕捉回原位 4) 不正确的文本会停留一秒钟左右 5) 每个单元格的文本都会翻转到正确的位置 从视觉上看,这种触发器非常烦人。无论如何,我觉得这是一种可以用一行我不知道的代码来
UIRefreshControl
)如果我要减慢mo的速度,那么:
假设有3个可见单元格,A、B和C,按从上到下的顺序排列
1) 我向下拉,桌面视图显示它令人耳目一新
2) A获取应该放在B中的文本。B获取应该放在C中的文本。C在屏幕外
3) “刷新”结束后,表格将捕捉回原位
4) 不正确的文本会停留一秒钟左右
5) 每个单元格的文本都会翻转到正确的位置
从视觉上看,这种触发器非常烦人。无论如何,我觉得这是一种可以用一行我不知道的代码来解决的问题
以下是我的代码摘录:
发生刷新时调用的函数(从CloudKit获取记录):
这是我的cellforrowatinexpath
函数:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if self.list.count == 1 && self.list[0].count == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: NO_POSTS, for: indexPath)
return cell
}
let post = self.list[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: POST_IN_FEED_CELL, for: indexPath)
if let postCell = cell as? PostInFeedTableViewCell {
// deleted the assignment of values to other UI elements in my custom cell
let predicate = NSPredicate(format: "%K = %@", LIBRARY_CODE, post.object(forKey: LIBRARY_CODE) as! String)
let query = CKQuery(recordType: LIBRARY_ITEM, predicate: predicate)
self.db.perform(query, inZoneWith: nil) { records, error in
if error == nil {
let libraryItem = records?[0]
DispatchQueue.main.async(execute: {
postCell.title.text = libraryItem?.object(forKey: NAME) as! String
})
}
else {
if let error = error as? CKError {
print(error)
}
}
}
}
return cell
}
不要在
cellForRowAtIndexPath
中执行CKQuery操作;这个函数需要快速执行。在显示单元格之前,您应该已经拥有了所需的所有数据。至少,在等待查询完成时,可以在单元格中放置某种占位符文本或清除文本或显示微调器,但也可以使用某种缓存,这样就不会一直获取相同的数据了,我明白了。谢谢这是有道理的,因为cellforrowatinexpath
必须执行很多次。这是我的一个次要问题,还是你认为它也能解决我的触发器问题?再次感谢!单元格外观不佳的问题通常是由单元格重用引起的,而且由于在返回单元格对象之前没有重置单元格内容(更新发生在闭包的后面),因此,如果是它导致了您的问题,我不会感到惊讶
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if self.list.count == 1 && self.list[0].count == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: NO_POSTS, for: indexPath)
return cell
}
let post = self.list[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: POST_IN_FEED_CELL, for: indexPath)
if let postCell = cell as? PostInFeedTableViewCell {
// deleted the assignment of values to other UI elements in my custom cell
let predicate = NSPredicate(format: "%K = %@", LIBRARY_CODE, post.object(forKey: LIBRARY_CODE) as! String)
let query = CKQuery(recordType: LIBRARY_ITEM, predicate: predicate)
self.db.perform(query, inZoneWith: nil) { records, error in
if error == nil {
let libraryItem = records?[0]
DispatchQueue.main.async(execute: {
postCell.title.text = libraryItem?.object(forKey: NAME) as! String
})
}
else {
if let error = error as? CKError {
print(error)
}
}
}
}
return cell
}