【问题标题】:Weird display behaviour when selecting tableview row选择 tableview 行时的奇怪显示行为
【发布时间】:2019-09-07 01:42:23
【问题描述】:

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


(来源:staticflickr.com

这是显示表格视图的代码

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) { }

}

从上面的代码来看,一切看起来都很正常,但是却出现了这种失真。请帮我看看为什么以及如何阻止它发生。

【问题讨论】:

  • 它没有扭曲,当你点击它时单元格会突出显示。
  • 将 Storyboard 中的 TableViewCell 属性 SelectionStyle 更改为无。

标签: swift uikit tableview


【解决方案1】:

正如评论所暗示的,这只是你的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
}

【讨论】:

  • 非常感谢!看起来很简单,但我不知道......
【解决方案2】:

是的,您应该将 tableView 选择样式设置为无。 1.在右侧面板中选择故事板中的tableview,将选择样式设置为无。 或者 2.在你的tableview cellforrow方法集

tableView.selectionstyle = none 

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多