There are one to three UITableViewCell
s in a UITableViewView
. Is there a way to always position the cell(s) at the bottom of screen after rel
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);
}
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 }
}