Friday, 18 September 2015

Display Table View Cell From Bottom

For the better user interface some time we need to display UITabelViewCell from bottom of the UITableView.
And, when we add new cell, its insert in bottom of UITableView and Table View auto scroll up for proper visualization for newly added cell.
We can use UITableView's contentInset for  this kind of behavior of UItableView.
Whenever add new cell update contentInset of Tableview.

Code
 -(void) updateTableViewInset:(UITableView *)tableView{
//First find out total number of cell in Table View
    NSInteger numRows=[tableView numberOfRowsInSection:0];

//Get height of Table View
    CGFloat contentInsetTop=tableView.bounds.size.height;
    for (int i=0;i<numRows;i++) {

//Get cell height and subtract by contentInsetTop
        contentInsetTop -= [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];

//Check if contentInsetTop reach to zero or less than zero
        if (contentInsetTop<=0) {
            contentInsetTop=0;
            break;
        }
    }

//update tableview contentInset.
    tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 50, 0);
}

No comments:

Post a Comment