2016-02-08 22 views
0

当我尝试使用下面的代码更新我的表**UITableview**时,我的单元格变得更小。我想知道是否有人面临同样的问题,并会知道为什么会发生这种情况。更新单元格时不一致的UITableViewCell大小

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]; 

....

+0

它可能是自动布局问题你检查它吗? –

+0

这是我怀疑的wha,但我不知道从哪里开始。它恢复正常,但我向下滚动。值得注意的是,我正在使用自定义的“TableViewCell”类。在setFrame中,我设置了一些 ' - (void)setFrame:(CGRect)frame frame.origin.x + = 5; frame.origin.y + = 5; frame.size.width - = 2 * 5; frame.size.height - = 2 * 5; [super setFrame:frame]; } ' – CreativityKills

+0

我发布了调整自动布局的答案。这可能会解决你的问题。 –

回答

0

可能是你还没有给出的UITableViewDelegate方法实现:

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

高度正确。我正在使用自定义单元格。 – CreativityKills

3

@CreativityKills。

如果您的自动布局已启用,请尝试添加行后添加以下方法。它可能会帮助你,如果它是自动布局的问题。

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]; 
    }]; 
} 

希望这可以帮助你。

+0

您好,非常感谢您的回复,我认为您在正确的轨道上,但高度已经很稳固,其宽度发生了变化。 – CreativityKills

+0

@CreativityKills可以将相同的功能更改为宽度。 –

+0

我试过了,并没有工作。我一起去除了插页,并按预期工作。 – CreativityKills

相关问题