4

我有自动布局的问题。我在UITableViewCell里面有一个和一个视图。我希望这个视图比单元格小,所以我添加了约束:

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

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

正确的单元格: 在此处输入图像描述

单元格不正确(滚动后) 在此处输入图像描述

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

提前感谢您的帮助!

4

3 回答 3

3

我终于在这里找到了我的问题的答案。在我的自定义 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];
    }
}

我猜内容视图会调整其内容的大小或类似的东西。如果对此有更简单的解释和更简单的方法,我仍在等待其他答案。非常感谢您的帮助!

于 2013-06-20T12:20:40.983 回答
1

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

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

    [tableViewCell.contentView setNeedsLayout];

    return tableViewCell;
}


真正的问题:

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

于 2013-11-27T18:11:10.933 回答
0

产生此问题是因为您的UITableViewReuse 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)
    {
    } 
于 2013-06-20T08:45:38.660 回答