Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 对UITableViewCells和动态高度使用自动布局_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

Ios 对UITableViewCells和动态高度使用自动布局

Ios 对UITableViewCells和动态高度使用自动布局,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,所以我已经看过了。但是我仍然无法正确地返回iOS7的高度。我有一个表格视图,其中有一个我在autolayout中布局的单元格,这相当复杂。每当我试图覆盖rowatinex的高度时(为了使其与iOS7兼容),我得到的行高度似乎计算不正确。当我不覆盖rowatinex的高度时,除了在iOS7上不起作用之外,一切都很好。我还尝试使用xibs自动布局整个内容。我的双胞格里真的没有任何逻辑。都在UITableView子类中 -(id)init{ //Implementation details g

所以我已经看过了。但是我仍然无法正确地返回iOS7的高度。我有一个表格视图,其中有一个我在autolayout中布局的单元格,这相当复杂。每当我试图覆盖rowatinex的
高度时(为了使其与iOS7兼容),我得到的行高度似乎计算不正确。当我不覆盖rowatinex的
高度时,除了在iOS7上不起作用之外,一切都很好。我还尝试使用xibs自动布局整个内容。我的双胞格里真的没有任何逻辑。都在UITableView子类中

-(id)init{
    //Implementation details go BELOW
    self.dataSource = self;
    self.delegate = self;
    [self registerNib:[UINib nibWithNibName:@"DipticCell" bundle:nil] forCellReuseIdentifier:@"dipticCell"];
    [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
    self.rowHeight = UITableViewAutomaticDimension;
    offScreenCells = [[NSMutableDictionary alloc] init];
    return self;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *reuseIdentifier = @"dipticCell";
    if (indexPath.row > 0){
        reuseIdentifier = @"cell2";
    }

    UITableViewCell *cell = [offScreenCells objectForKey:reuseIdentifier];
    if (!cell) {
        cell = [self dequeueReusableCellWithIdentifier:reuseIdentifier];
        [offScreenCells setObject:cell forKey:reuseIdentifier];
    }


    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];


    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));


    [cell setNeedsLayout];
    [cell layoutIfNeeded];

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    height += 1.0f;
    return height;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0){
        DipticCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dipticCell"];
        if (cell == nil) {
            cell = [[DipticCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dipticCell"];
        }
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell2"];
        }
        cell.textLabel.text = @"one";
        return cell;
    }

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
只是为了确认所有内容都在contentView中

此外,当我重写RowAtIndexPath的高度时,我会收到“无法同时满足约束”警告。但当我不覆盖它时,事情似乎就正常了(这是有意义的,因为单元格应该只适应内容的高度,现在我尝试用1的高度覆盖它。)


如果您想运行该项目,这里有一个指向该项目的链接

大多数时候对我有效的是确保有一条从单元格顶部到单元格底部的约束路径

因此,如果您的单元格显示为三个标签:label1、label2、label3和所有标签都应显示在彼此下方。然后添加约束:

cell.top to label1.top
label1.bottom to label2.top
label2.bottom to label3.top
label3.bottom to cell.bottom

这样,单元格的高度由约束定义。因此,请确保连接所有定义此链中单元高度的子视图。

链接的项目只是一个空视图。没有桌子,修好了。我不知道为什么第一次没有正确地推送它…如果我在下载后直接在iOS 8模拟器上运行项目,它就不会正确显示。红色文本单元格被压缩。在iOS 8上看起来不是很好吗?还有,UIScrollView的用途是什么?现在好像空了。有时滚动视图在放入单元格时会出现问题(因为UITableView是UIScrollView的子类),您不能删除它吗?*“红色文本单元格”应为“红色文本视图”。我仔细检查了一下,一切看起来都很好。就像我说的,当我去掉rowatinexpath位的heightforrowatinexpath位时,它是有效的,但是当我把它加回去时,它错误地估计了高度。