17

我正在UITableView使用estimatedRowHeightUITableViewAutomaticDimension。我也NSFetchedResultsControllerDelegate用来反映变化。

一切正常。现在的问题是当我添加一些记录时CoreDataNSFetchedResultsController称为它的委托,但会发生意想不到的事情。TableView 每次都会突然滚动到 Top。

NSFetchedResultsControllerDelegate

func controllerWillChangeContent(controller: NSFetchedResultsController) {
    tableView.beginUpdates()
}

func controllerDidChangeContent(controller: NSFetchedResultsController) {
    tableView.endUpdates()
}

func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
        switch type {
        case .Insert:
            tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
            break

        case .Update:
            tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
            break

        case .Delete:
            tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
            break

        default: break;
        }
    }

通过谷歌搜索,我发现人们建议使用的答案很少,tableView: heightForRowAtIndexPath:但因为我的单元格高度是动态的。所以我该怎么做?

4

7 回答 7

7

这是 tableview 的默认行为,在 tableview 中插入一行会将其滚动到顶部。我的应用程序确实遇到了同样的问题。这是我如何管理tableview的内容偏移量,请按照步骤操作,

1)在插入行或节之前存储UITableview的内容偏移量。

2)在数组中插入对象。

3)重新加载表格视图

4)减去差值并手动设置内容偏移量。

let oldOffset: CGFloat = tblTest.contentSize.height
tblTest.reloadData()
let newOffset: CGFloat = tblTest.contentSize.height
tblTest.setContentOffset(CGPointMake(0, (newOffset - oldOffset)), animated: false)

到目前为止,这就是我在我的应用程序中所做的,并且使用动态单元格来克服每次重新初始化单元格,它的工作非常棒。

于 2016-10-14T12:15:03.280 回答
4

为了解决我的问题,我将单元格高度保存在willDisplay方法中并在estimatedHeightForRowAt检索值中。

var cellHeights = NSMutableDictionary()

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    if let height = cellHeights.object(forKey: indexPath) {
        return height as! CGFloat
    }

    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cellHeights.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
}
于 2016-10-13T09:52:13.863 回答
4

添加到@jayesh-miruliya 的答案,在您NSFetchedResultsControllerDelegate的 switch 语句之后,将其放入您的代码中:

tableView.reloadData()
dispatch_async(dispatch_get_main_queue(), {
    let topCellIndexPath = NSIndexPath(forRow: 0, inSection: 0)
    self. tableView.scrollToRowAtIndexPath(topCellIndexPath, atScrollPosition: .Top, animated: true)
})
于 2016-05-04T07:11:06.093 回答
1
  func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:

      tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .None)
        break

    case .Update:
        tableView.reloadRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
        break

    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .None)
        break

    default: break;
    }
   tableView.reloadData()
  let indexPath = NSIndexPath(forRow: 0, inSection: 0)
  self. tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
   }

}
于 2016-05-09T10:15:06.770 回答
1
tableView.reloadData()
if tableView.numberOfRowsInSection(0) > 0
{
      let indexPath = NSIndexPath(forRow: 0, inSection: 0)
      self. tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}
于 2016-05-03T10:33:46.170 回答
-1
let cou : Int = self.Array.count as Int
let lastindex : NSIndexPath = NSIndexPath(forRow: cou-1, inSection: 0)
self.TableName.scrollToRowAtIndexPath(lastindex, atScrollPosition: UITableViewScrollPosition.None, animated: true)

此代码可帮助您在特定位置滚动表格视图。

于 2016-10-15T04:47:38.120 回答
-1
func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    tabelView.reloadData()
}

这对我有用

于 2016-11-03T07:08:25.403 回答