Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/36.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
Iphone 最终表视图:高度为行索引路径:解决方案_Iphone_Ios_Objective C_Uitableview - Fatal编程技术网

Iphone 最终表视图:高度为行索引路径:解决方案

Iphone 最终表视图:高度为行索引路径:解决方案,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,好吧,这是一个几乎每个iOS开发者都在努力解决的老问题。关于这个问题有很多答案 但是,我仍然没有找到一个真正实用的通用解决方案来计算tableView中UITableViewCell的高度:heightForRowAtIndexPath: 我了解UITableView的布局机制是如何工作的,它在实际布局任何单元格之前计算整个表格的高度。 我也明白,人们基本上必须预测UITableViewCells布局方法中会发生的一切,例如sizeWithFont:。。。NSString上的方法 我假设单元格的

好吧,这是一个几乎每个iOS开发者都在努力解决的老问题。关于这个问题有很多答案

但是,我仍然没有找到一个真正实用的通用解决方案来计算tableView中UITableViewCell的高度:heightForRowAtIndexPath:

我了解UITableView的布局机制是如何工作的,它在实际布局任何单元格之前计算整个表格的高度。 我也明白,人们基本上必须预测UITableViewCells布局方法中会发生的一切,例如sizeWithFont:。。。NSString上的方法

我假设单元格的样式是UITableViewCellStyleSubtitle,我们可以在以后得到更通用的格式

这是我的要求:

该单元格可能位于分组或普通表视图中 系统版本可能是iOS 6或iOS 7+ 可以在cell.imageView中设置图像,也可以不设置图像 可以设置附件视图,也可以不设置附件视图 cell.textLabel可能包含也可能不包含文本。 cell.detailTextLabel可能包含或不包含文本。 字体可以自定义 标签可能是多行的 桌子的宽度是任意的肖像,风景,iPad。。。 细胞的数量有限,大概有十几个 在大多数情况下,这种动态单元格将用于短列表中,可能是某种详细视图,因此我认为可以像下面所述那样预先计算单元格:

我正在寻找一个实现来计算满足给定要求的单元高度。
如果有人能分享一些见解和经验,我将非常高兴-提前感谢

制作具有不同高度的自定义单元格的最佳方法是:

为单元格创建NSMutableArray:

@property (nonatomic,retain)NSMutableArray* cellsArray;
然后在m中:

调用的第一个方法是

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
在此处创建您的手机:

    -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (!self.cellsArray) {

            self.cellsArray = [[NSMutableArray alloc]init];

        }

        ListCell* cell = [[ListCell alloc] initWithFrame:CGRectMake(x, y, width, height)];


       // IMPLEMENT your custom cell here, with custom data, with custom,different height:

       // after add this cell to your array:
        [self.cellsArray addObject:cell];

       // IMPORTANT STEP:  set your cell height for the new height:

        cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, 303, yourLastItemInCell.frame.origin.y + yourLastItemInCell.frame.size.height);


        return cell.frame.size.height;
    }
获得具有不同高度的自定义单元格后:

#pragma mark - conform to delegates

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    return self.cellsArray[indexPath.row];

}
我希望有帮助

编辑:


您可以在表视图的委托中保留一个虚拟单元格,并让它计算自己所需的大小。

我认为这无法满足“单元格可能位于分组或普通表视图中”的要求,是吗?缺少的元素是否是预期的:“CGRectMake0,0,”,,50'?啊,好的–那么你建议为分组样式案例实现我自己的分组样式绘图和布局?如果你实现你自己的tableview和单元格,你可以根据需要定制一个单元格。高度不会有问题!
tableView = [[UITableView alloc] initWithFrame:frame style: UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;