UITableViewCell自动布局

时间:2013-06-20 07:59:36

标签: iphone objective-c xcode autolayout

我遇到autolayout问题。我有一个UITableViewCell和一个视图。我希望这个视图比单元格小,所以我添加了约束:

  • 垂直空间(10)顶部
  • 垂直空间(10)底部
  • 水平空间(10)前方空间
  • 水平空间(10)尾随空格

当tableView出现时,单元格显示良好,但是如果我向下滚动tableView并向上滚动,单元格中的视图将调整大小,现在占用单元格中的空间。

纠正细胞: enter image description here

不正确的单元格(滚动后) enter image description here

我已经搜索过这个,但找不到类似的东西。 我认为这个人有同样的问题,但没有答案: Similar problem

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

我终于找到了问题here的答案。在我的自定义UITableViewCell类中,我这样做了:

- (void)awakeFromNib
{
    [super awakeFromNib];

    for (NSLayoutConstraint *cellConstraint in self.constraints)
    {
        [self removeConstraint:cellConstraint];

        id firstItem = cellConstraint.firstItem == self ? self.contentView : cellConstraint.firstItem;
        id seccondItem = cellConstraint.secondItem == self ? self.contentView : cellConstraint.secondItem;

        NSLayoutConstraint* contentViewConstraint = [NSLayoutConstraint constraintWithItem:firstItem
                                                                                 attribute:cellConstraint.firstAttribute
                                                                                 relatedBy:cellConstraint.relation
                                                                                    toItem:seccondItem
                                                                                 attribute:cellConstraint.secondAttribute
                                                                                multiplier:cellConstraint.multiplier
                                                                                  constant:cellConstraint.constant];

        [self.contentView addConstraint:contentViewConstraint];
    }
}

我猜内容视图会调整其内容或类似内容。如果有一个更容易解释这个和更简单的方法这样做,我还在等待其他答案。非常感谢您的帮助!

答案 1 :(得分:1)

今天我使用Xcode 5和故事板遇到了同样的问题。最初,我的自定义表格视图单元格会正确显示,但当我滚动浏览表格视图并重复使用单元格时,布局会变得混乱。修复是像这样调用setNeedsLayout

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingTableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Setting" forIndexPath:indexPath];

    [tableViewCell.contentView setNeedsLayout];

    return tableViewCell;
}


真正的问题:

我将我的标签插座命名为textLabel,其名称与UITableViewCell的内置标签属性相同。这引起了各种各样的怪异。只要我将插座属性名称更改为唯一名称,我的布局问题就解决了。

答案 2 :(得分:0)

生成此问题是因为您的UITableView重新使用Cell即时创建新的。 我给你一些可能对你有帮助的建议。

1)在

之间添加UIView
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
  {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        /// Your code;
    }
 return cell;

}

1)将你的UIView添加到cell.contentView.

<强>编辑:

在您按照我的编辑答案之前,我想告诉您,以下代码对内存管理不利,因为它会为UITableView的每一行创建新单元格,所以请小心。

但如果适用于UITableView有限行(50-100可能),则使用以下代码会更好。

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
    }