我有这个 TableView ,它通过核心数据填充。一切看起来都很好,即使在滚动时也是如此,直到我按住任何行 - 然后该行就会像下面的第二张图片一样扭曲。
(来源:staticflickr.com)
(来源:staticflickr.com)
这是显示表格 View 的代码
override func viewDidLoad() {
super.viewDidLoad()
animateBackground(colors)
tableView.delegate = self
tableView.dataSource = self
tableView.fetchData(fetchedResultsController as! NSFetchedResultsController<NSFetchRequestResult>)
self.tableView.tableFooterView = UIView(frame: .zero)
}
func numberOfSections(in tableView: UITableView) -> Int {
guard fetchedResultsController.sections?.count != nil else { return 0 }
return fetchedResultsController.sections!.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard fetchedResultsController.sections != nil else { return 0 }
let data = fetchedResultsController.sections![section]
return data.numberOfObjects
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SkillCell", for: indexPath) as! SkillCell
configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
return cell
}
func configureCell(cell: SkillCell, indexPath: NSIndexPath) {
let skill = fetchedResultsController.object(at: indexPath as IndexPath)
cell.skill = skill
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { }
}
从上面的代码来看,一切看起来都很正常,但是却出现了这种扭曲。请帮我看看为什么以及如何阻止它发生。
最佳答案
正如评论所暗示的,这只是 UITableViewCell
的一个名为 selectionStyle
的视觉效果。在您的 cellForRowAt indexPath
方法中,您可以禁用它,如下所示:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! MyCell
cell.selectionStyle = .none // this removes the effect.
return cell
}
关于swift - 选择 TableView 行时出现奇怪的显示行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57830057/