如何计算通过cellForRowAtIndexPath中的开关设置的单元格的heightForRowAtIndexPath.

Den*_*989 3 iphone objective-c uitableview ios

我设置我的单元格如下:

因此,一些开关定义单元格,因为数据不在对象列表中,而是一组信息,应以某种不同的方式显示在tableView中.

-(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;
}
Run Code Online (Sandbox Code Playgroud)

我像这样计算高度.

 -(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);
Run Code Online (Sandbox Code Playgroud)

问题:

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

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

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

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

小智 5

方法

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

在方法之前为每个单元调用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

因此,在第一种方法中,尚未创建单元格,您不应尝试访问它们.

创建tableView时,首先会询问数据源的行数.然后,对于每一行,要求数据源输入行的高度,以便tableView知道其内容的总高度,最后,要求数据源显示单元格(实际视图).

在你的情况下,我会再次构建开关盒结构.关于"数据源列表",我以前从未这样做过,所以也许这是一个更好的解决方案.