How to position UITableViewCell at bottom of screen?

前端 未结 14 601
旧时难觅i
旧时难觅i 2020-12-08 03:25

There are one to three UITableViewCells in a UITableViewView. Is there a way to always position the cell(s) at the bottom of screen after rel

14条回答
  •  悲哀的现实
    2020-12-08 03:37

    func updateContentInsetForTableView( tableView:UITableView,animated:Bool) {
    
        let lastRow = tableView.numberOfRows(inSection: 0)
        let lastIndex = lastRow > 0 ? lastRow - 1 : 0;
    
        let lastIndexPath = IndexPath(row: lastIndex, section: 9)
    
        let lastCellFrame = tableView.rectForRow(at: lastIndexPath)
        let topInset = max(tableView.frame.height - lastCellFrame.origin.y - lastCellFrame.height, 0)
    
        var contentInset = tableView.contentInset;
        contentInset.top = topInset;
    
        _ = UIViewAnimationOptions.beginFromCurrentState;
        UIView.animate(withDuration: 0.1, animations: { () -> Void in
    
            tableView.contentInset = contentInset;
        })
    
    
        if self.commesnts.count > 0 {
            tableView.scrollToRow(at: IndexPath(item:self.commesnts.count-1, section: 0), at: .bottom, animated: true)
        }
    
    }
    

    I used @bkokot solution with little addition. It does two things

    1. Start showing cells from bottom of UITableView
    2. Scroll cells to bottom so that last inserted row become visible
    (just like chat)
    

提交回复
热议问题