iOS,自定义单元格的边距在标题中不同于行,具有自动布局?

时间:2015-05-04 21:54:59

标签: ios uitableview cocoa-touch autolayout

我有一个问题。我使用nib文件作为表行,并使用相同的nib作为节头。我也在我的自定义单元格上使用自动布局。

虽然我使用不同的文字和颜色等。

我的想法是,我可以对齐列等。

然而,虽然背景看起来是全宽的,但节标题似乎比单元格更宽。

我已经尝试了各种各样的设置宽度,认为它的设置不同,有余量。

这是我的代码......

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:
   (NSInteger)section 
{
    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    HeaderSectionReportView *customHeaderView = [[HeaderSectionReportView alloc] 
         initWithFrame:CGRectMake(0.0, 0.0, w, 20.0) ....

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ReportCell" 
         owner:self options:nil];
    ReportCell *customView  = (ReportCell *)[nib objectAtIndex:0];

    customView.descriptionValue.backgroundColor = [UIColor clearColor];
    //snip
    customView.frame = CGRectMake(0,0, w,customView.frame.size.height);
    [customHeaderView addSubview:customView];
    return customHeaderView;
}

//

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
    (NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"ReportCell";
    ReportCell *cell = (ReportCell *) [tableView 
         dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] 
            loadNibNamed:@"ReportCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = ((ReportCell *) currentObject);
                break;
            }
        }
    }

    CGFloat w = [UIScreen mainScreen].bounds.size.width;
    cell.frame = CGRectMake(0,0, w,cell.frame.size.height);    
    cell.descriptionValue.text = desc;
}

此处还有我的自动布局限制 enter image description here

1 个答案:

答案 0 :(得分:0)

标题和单元格的宽度由tableView处理。因此设置cell.frame不会有任何区别。

因此,您对细胞的约束似乎有些偏差。它们都应设置为0且没有任何余量(禁用/取消相对于边距)。

enter image description here

希望这可以解决您的问题。如果您需要更多帮助,请告诉我