UITableView 仅在向上滚动时更新,而不是向下滚动

UITableView only updating on scroll up, not down

我有一个 UITableView,当我向上滚动时它会更新,但当我向下滚动时它不会更新。此外,当它确实更新时,它偶尔似乎 "skip" 一个单元格并更新下一个单元格。

  1. 总共应填充 6 个单元格
  2. 我已经在故事板中创建了 UITableView,在故事板中为 hashLabel 和 creditLabel 设置了我的约束

这是初始 TableView 的图像:

向上滚动,正确更新后:

...向上滚动时 "misses" 一个单元格:

当然还有 class:

class HashtagController: UIViewController, UITableViewDelegate,     UITableViewDataSource  {

  var model:ModelData!
  var currentCell: UITableViewCell!
  @IBOutlet var hashtagTableView: UITableView!
  let basicCellIdentifier = "CustomCells"

  override func viewDidLoad() {
    super.viewDidLoad()
    model = (self.tabBarController as CaptionTabBarController).model
    hashtagTableView.delegate = self
    hashtagTableView.dataSource = self
    self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CherrySwash-Regular", size: 25)!,  NSForegroundColorAttributeName: UIColor(red:27.0/255, green: 145.0/255, blue: 114.0/255, alpha: 1.0)]
    configureTableView()
    hashtagTableView.reloadData()
  }

  func configureTableView() {
    hashtagTableView.rowHeight = UITableViewAutomaticDimension
    hashtagTableView.estimatedRowHeight = 160.0
  }

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    //deselectAllRows()
    hashtagTableView.reloadData()
  }

  override func viewDidAppear(animated: Bool) {
    hashtagTableView.reloadData()
  }

  func deselectAllRows() {
    if let selectedRows = hashtagTableView.indexPathsForSelectedRows() as? [NSIndexPath] {
      for indexPath in selectedRows {
        hashtagTableView.deselectRowAtIndexPath(indexPath, animated: false)
      }
    }
  }

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return model.quoteItems.count
  }

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return customCellAtIndexPath(indexPath)
  }

  func customCellAtIndexPath(indexPath:NSIndexPath) -> CustomCells {
    var cell = hashtagTableView.dequeueReusableCellWithIdentifier(basicCellIdentifier) as CustomCells
    setTitleForCell(cell, indexPath: indexPath)
    setSubtitleForCell(cell, indexPath: indexPath)
    return cell
  }

  func setTitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
    let item = Array(Array(model.quoteItems.values)[indexPath.row])[0] as? String
    cell.hashLabel.text = item
  }

  func setSubtitleForCell(cell:CustomCells, indexPath:NSIndexPath) {
    let item = Array(model.quoteItems.keys)[indexPath.row]
    cell.creditLabel.text = item
  }

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    /*currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
    var currentLabel = currentCell.textLabel?.text
    var currentAuthor = currentCell.detailTextLabel?.text

    model.quote = currentLabel!
    model.author = currentAuthor!*/
  }
}

class CustomCells: UITableViewCell {
  @IBOutlet var hashLabel: UILabel!
  @IBOutlet var creditLabel: UILabel!
}

事实证明,这个问题与我的 estimatedRowHeight 有关。在这种情况下,行高太大,影响了 table 单元格的构建方式。

所以最后我将 hashtagTableView.estimatedRowHeight = 160.0 更改为 hashtagTableView.estimatedRowHeight = 80.0,一切正常。