How to position UITableViewCell at bottom of screen?

前端 未结 14 572
旧时难觅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:46

    Call this method whenever a row is added:

    - (void)updateContentInset {
        NSInteger numRows=[self tableView:_tableView numberOfRowsInSection:0];
        CGFloat contentInsetTop=_tableView.bounds.size.height;
        for (int i=0;i<numRows;i++) {
            contentInsetTop-=[self tableView:_tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
            if (contentInsetTop<=0) {
                contentInsetTop=0;
                break;
            }
        }
        _tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0);
    }
    
    0 讨论(0)
  • 2020-12-08 03:49

    This can be done by in swift using below funcation

    func updateContentInsetForTableView(tblView: UITableView, animated: Bool) {
    
        let lastRow: NSInteger = self.tableView(tblView, numberOfRowsInSection: 0)
        let lastIndex: NSInteger = lastRow > 0 ? lastRow - 1 : 0
        let lastIndexPath: NSIndexPath = NSIndexPath(forRow: lastIndex, inSection: 0)
        let lastCellFrame: CGRect = tblView.rectForRowAtIndexPath(lastIndexPath)
        let topInset: CGFloat = max(CGRectGetHeight(tblView.frame) - lastCellFrame.origin.y - CGRectGetHeight(lastCellFrame), 0)
        var contentInset: UIEdgeInsets = tblView.contentInset
        contentInset.top = topInset
        let option: UIViewAnimationOptions = UIViewAnimationOptions.BeginFromCurrentState
        UIView.animateWithDuration(animated ? 0.25 : 0.0, delay: 0.0, options: option, animations: { () -> Void in
            tblView.contentInset = contentInset
        }) { (_) -> Void in }
    }
    
    0 讨论(0)
提交回复
热议问题