如何计算通过cellForRowAtIndexPath中的开关设置的单元格的heightForRowAtIndexPath。 [英] How to calculate heightForRowAtIndexPath for cells which setup via switch in cellForRowAtIndexPath.

查看:109
本文介绍了如何计算通过cellForRowAtIndexPath中的开关设置的单元格的heightForRowAtIndexPath。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置我的单元格如下:

I setup my cells like this:

所以,有几个开关定义单元格,因为数据不在对象列表中,而是一组应以不同方式显示在tableView中的信息。

So, a couple of switches, define the cells, because the data is not in the list of objects, but a set of information which should be displayed in a tableView in some different ways.

-(UITableViewCell *)value1CellForTableView:(UITableView *)tableView {
    static NSString *CellIdentifierValue1 = @"Value1Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierValue1];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifierValue1];
        cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.textAlignment = UITextAlignmentLeft;
    }
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;

    switch (indexPath.section) {
        case 0:
            //Coupon
            switch (indexPath.row) {
                case 0:
                    //Couponcode
                    cell = [self value1CellForTableView:tableView];
                    cell.textLabel.text = @"Code";
                    cell.detailTextLabel.text = presentedCoupon.couponNr;
                    break;
                case 1:
                    //Coupondescription
                    cell = [self value1CellForTableView:tableView];
                    cell.detailTextLabel.text = presentedCoupon.couponDescription;
                    cell.detailTextLabel.numberOfLines = 0;
                    cell.textLabel.text =@"Ihr Vorteil";
                    break;

            }        
            break;


        case 1:
            //Productinfo
            switch (indexPath.row) {
                case 0:
                    cell = [self defaultCellForTableView:tableView];
                    cell.imageView.image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath]  stringByAppendingPathComponent: [presentedCoupon.refProdName stringByAppendingString:@".png"]]];
                    cell.textLabel.text = presentedCoupon.refProdName;
                    break;



            }
            break;  

        case 2:
            //Shopinfo
            switch (indexPath.row) {
                case 0:
                    cell = [self defaultCellForTableView:tableView];
                    cell.textLabel.text = ((Shop*)presentedCoupon.refShop).name;
                    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

                    break;    
            }
            break;       
    }

    if (cell == nil) {
        cell = [self defaultCellForTableView:tableView];
        cell.textLabel.text = @"Stanni";
    }
    [cell layoutIfNeeded];
    return cell;
}

我计算这样的高度。

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        NSLog(@"height");
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

    CGFloat height = 24 + [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize: CGSizeMake(cell.detailTextLabel.frame.size.width, 1000.0f) lineBreakMode:cell.detailTextLabel.lineBreakMode].height;

    return MAX(height, 44.0f);

问题:

问题是,如许多线程所述,并且在我的日志中也可见,在表视图的初始化时会询问每个单元格的高度(可见或不可见)。因此,在更大的100多个列表中,还会创建100多个单元 - >在启动时浪费。

The problem is, as mentioned in many threads, and also visible in my log, that the height of each cell, (visible or not) is asked at the initialization of the table view. So in bigger 100+ lists, also 100+ cells are created --> wasted at startup.

当单元格设置如此时,还有另一种可能获取此信息吗?是否真的有必要在heightForRowAtIndexPath中再次构建switch case sturcture以避免这些调用,尽管每个单元的高度都合适?

Ist there another possibility to get this information when the cell is set up like this? Is it really neccessary to built the switch case sturcture again in heightForRowAtIndexPath to avoid these calls and though get the right heights for each cell?

使用每个单元格的单个信息保存数据源列表会更好吗?

Would it be better to hold a "datasource-list" with the single information of each cell?

但是如何处理不同的单元格样式,自定义单元格。

But how to handle the different cell styles, custom cells.

推荐答案

两种可能性:


  1. 你有多少个细胞?如果您有少量单元格(这里似乎就是这种情况),您不需要重复使用单元格!通常,细胞重用过度使用。
    只需在创建控制器或更新数据并将其放入 NSArray 时创建单元格。

    然后你可以从 tableView:cellForRowAtIndexPath:返回它们或测量它们的高度。

  1. How many cells you have? If you have a small number of cells (which seems to be the case here), you don't need cell reusing! In general, cell reusing is overused. Just create cells when you are creating your controller or when you have updated your data and put them into a NSArray.
    Then you can return them from tableView:cellForRowAtIndexPath: or measure their height.

创建一个返回单元格的单独方法text / font并在两个委托方法中使用它,而不是直接从单元格中读取信息。

Create a separate method that returns cell text/font and use it in both delegate methods instead of reading the information from the cell directly.

这篇关于如何计算通过cellForRowAtIndexPath中的开关设置的单元格的heightForRowAtIndexPath。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆