Error: UITableView jump to top with UITableViewAutomaticDimension

前端 未结 7 1693
半阙折子戏
半阙折子戏 2020-12-05 15:17

I am using UITableView with estimatedRowHeight and UITableViewAutomaticDimension. Also I am using NSFetchedResultsControllerDele

相关标签:
7条回答
  • 2020-12-05 15:19
      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)
       }
    
    }
    
    0 讨论(0)
  • 2020-12-05 15:20

    To solve my problem I save the cell height in the willDisplay method and in estimatedHeightForRowAt I'm retrieving the value.

    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)
    }
    
    0 讨论(0)
  • 2020-12-05 15:21
    tableView.reloadData()
    if tableView.numberOfRowsInSection(0) > 0
    {
          let indexPath = NSIndexPath(forRow: 0, inSection: 0)
          self. tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
    }
    
    0 讨论(0)
  • 2020-12-05 15:23
    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)
    

    This code help you to scroll your tableview at particular position.

    0 讨论(0)
  • 2020-12-05 15:24
    func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        tabelView.reloadData()
    }
    

    this is work for me

    0 讨论(0)
  • 2020-12-05 15:39

    Adding to @jayesh-miruliya 's answer, in your NSFetchedResultsControllerDelegate after the switch statement, put this in your code:

    tableView.reloadData()
    dispatch_async(dispatch_get_main_queue(), {
        let topCellIndexPath = NSIndexPath(forRow: 0, inSection: 0)
        self. tableView.scrollToRowAtIndexPath(topCellIndexPath, atScrollPosition: .Top, animated: true)
    })
    
    0 讨论(0)
提交回复
热议问题