swift - 选择 TableView 行时出现奇怪的显示行为

标签 swift uikit tableview

我有这个 TableView ,它通过核心数据填充。一切看起来都很好,即使在滚动时也是如此,直到我按住任何行 - 然后该行就会像下面的第二张图片一样扭曲。 normal view
(来源:staticflickr.com)

weird distortion
(来源: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/

相关文章:

ios - 如何将 Cocoapod 导入 App Extension?

swift - 如何搜索[[String]]类型的字符串或字符

ios - 自定义 UIResponder 子类

UIImage 上的 Swift 模糊扩展崩溃

ios - Swift Azure Mobile 快速入门 - 添加和同步 PNG 图像

swift - Xcode 8 Storyboard 项目不可选择

iOS 键盘键移出屏幕顶部

css - JavaFX tableview 删除默认的备用行颜色

swift - 当我在 TableView 中向下滚动时,单元格数据发生变化 - Swift 4

tableview - JavaFX-8 - 如何获取 TableView 的行数?