UITableview Scrolls to Top on Reload

后端 未结 5 1279
不知归路
不知归路 2020-12-28 18:23

I am facing problem in my app - you can post and edit your favorite places. After posting a post, or editing a specific post (UITableViewCell), UITablevie

相关标签:
5条回答
  • 2020-12-28 18:25

    Just go for these lines if you want an easy solution

    let contentOffset = tableView.contentOffset
    tableView.reloadData()
    tableView.setContentOffset(contentOffset, animated: false)
    
    0 讨论(0)
  • 2020-12-28 18:36

    In swift 3.1

    DispatchQueue.main.async(execute: {
    
        self.TableView.reloadData()
        self.TableView.contentOffset = .zero
    
    })
    
    0 讨论(0)
  • 2020-12-28 18:42

    Igor's answer is correct if you are using dynamically resizable cells (UITableViewAutomaticDimension)

    Here it is in swift 3:

        private var cellHeights: [IndexPath: CGFloat?] = [:]
        var expandedIndexPaths: [IndexPath] = []
    
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            cellHeights[indexPath] = cell.frame.height
        }
    
        func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            if let height = cellHeights[indexPath] {
                return height ?? UITableViewAutomaticDimension
            }
            return UITableViewAutomaticDimension
        }
    
    
        func expandCell(cell: UITableViewCell) {
          if let indexPath = tableView.indexPath(for: cell) {
            if !expandedIndexPaths.contains(indexPath) {
                expandedIndexPaths.append(indexPath)
                cellHeights[indexPath] = nil
                tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
                //tableView.scrollToRow(at: indexPath, at: .top, animated: true)
            }
          }
        }
    
    0 讨论(0)
  • 2020-12-28 18:45

    To prevent scrolling to top, you should save heights of cells when they loads and give exact value in tableView:estimatedHeightForRowAtIndexPath:

    // declare cellHeightsDictionary
    NSMutableDictionary *cellHeightsDictionary;
    
    // initialize it in ViewDidLoad or other place
    cellHeightsDictionary = @{}.mutableCopy;
    
    // save height
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
        [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];
    }
    
    // give exact height value
    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSNumber *height = [cellHeightsDictionary objectForKey:indexPath];
        if (height) return height.doubleValue;
        return UITableViewAutomaticDimension;
    }
    
    0 讨论(0)
  • 2020-12-28 18:49

    The UITableView's reloadData() method is explicitly a force reload of the entire tableView. It works well, but is usually jarring and a bad user experience if you're going to do that with a tableview that the user is currently looking at.

    Instead, take a look at reloadRowsAtIndexPaths(_:withRowAnimation:) and reloadSections(_:withRowAnimation:) in the documentation.

    0 讨论(0)
提交回复
热议问题