iOS 8 Auto cell height - Can't scroll to last row

前端 未结 12 1110
慢半拍i
慢半拍i 2020-11-28 20:50

I am using iOS 8 new self-sizing cells. Visually it works good - each cell gets its right size. However, if I try to scroll to the last row, the table view

相关标签:
12条回答
  • 2020-11-28 21:27

    For my case, I found a temporary workaround by not suggesting an estimated cell height to the program. I did this by commenting out the following method in my code:

    - (CGFloat) tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    However, please take note that doing so may affect the user experience when the user scrolls, if your cells varies a lot compared to each other. For my case, no noticeable difference so far.

    Hope it helps!

    0 讨论(0)
  • 2020-11-28 21:27

    I have this problem in Swift 5 iOS 13 yet, this solved my problem

     DispatchQueue.main.async { [weak self] in
        self?.tableView.reloadData()
        self?.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
     }
    
    0 讨论(0)
  • 2020-11-28 21:29

    This has been a very annoying bug, but I think I found a permanent solution, though I cannot fully explain why.

    Call the function after a tiny (unnoticed) delay:

    let delay = 0.1 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
    
    dispatch_after(time, dispatch_get_main_queue(), {
      tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: model.dataArray.count - 1, inSection: 0), atScrollPosition: .Bottom, animated: true)
    })
    

    Do tell me if this works for you as well.

    0 讨论(0)
  • 2020-11-28 21:31

    My solution was to use the size of the storyboard as the estimate.

    So instead of this:

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {   
    return UITableViewAutomaticDimension;
    

    }

    I did something like this:

    - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    
    MyMessageType messageType = [self messageTypeForRowAtIndexPath:indexPath];
    
    switch (messageType) {
    
        case MyMessageTypeText:
            return 45;
            break;
    
        case MyMessageTypeMaybeWithSomeMediaOrSomethingBiggerThanJustText:
            return 96;
            break;
    
        default:
            break;
     }
    }
    

    I'm writing a chat table view so it is likely that many of my cells, specifically that text type will be larger than what is in IB, especially if the chat message is very long. This seems to be a pretty good...well...estimate and scrolling to the bottom gets pretty close. It seems to be slightly worse as the scrolling gets longer, but that is to be expected I suppose

    0 讨论(0)
  • 2020-11-28 21:33

    Use this simple code to scroll bottom

     var rows:NSInteger=self.tableName.numberOfRowsInSection(0)
            if(rows > 0)
            {
                let indexPath = NSIndexPath(forRow: rows-1, inSection: 0)
                tableName.scrollToRowAtIndexPath(indexPath , atScrollPosition:  UITableViewScrollPosition.Bottom, animated: true)
            }
                }
    
    0 讨论(0)
  • 2020-11-28 21:38

    Just call tableview reloadData after viewDidAppear can solve the problem

    -(void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [self.tableView reloadData];
    }
    
    0 讨论(0)
提交回复
热议问题