CreativityKills February 2016

Inconsistent UITableViewCell Size When Updating Cells

When I attempt to update my table **UITableview** using the code below my cells become smaller. I was wondering if anyone has faced the same problem and would know why this is happening.

 NSMutableArray* indexPaths = [[NSMutableArray alloc] init];
 for (int i = 20; i < 20 + 20; i++) {
     [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
 }

 [self.tableView beginUpdates];
 [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationBottom)];
 [self.tableView endUpdates];

....

Answers


Prabhu.Somasundaram February 2016

May be you have not given implementation for UITableViewDelegate method:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{
    return 44.0;//your default row height
}


Ketan P February 2016

@CreativityKills.

if your auto layout is on please try following method to add after add rows. It may help you if it was a problem of auto-layout.

 NSMutableArray* indexPaths = [[NSMutableArray alloc] init];
 for (int i = 20; i < 20 + 20; i++) {
     [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
 }

 [self.tableView beginUpdates];
 [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:(UITableViewRowAnimationBottom)];
 [self.tableView endUpdates];
 [self adjustHeightOfTableview];


- (void)adjustHeightOfTableview
{
    CGFloat height = self.tableView.contentSize.height;
    CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;

    // if the height of the content is greater than the maxHeight of
    // total space on the screen, limit the height to the size of the
    // superview.

    if (height > maxHeight)
        height = maxHeight;

    // now set the height constraint accordingly

    [UIView animateWithDuration:0.25 animations:^{
        self.tableViewHeightConstraint.constant = height;
        [self.view setNeedsUpdateConstraints];
    }];
}

Hope this may help you.

Post Status

Asked in February 2016
Viewed 2,591 times
Voted 14
Answered 2 times

Search




Leave an answer