表格视图单元格在单击细胞时更改高度

时间:2014-11-15 22:13:39

标签: ios objective-c uitableview ios8 autolayout

我有一个带有3个自定义单元格的 TableView

enter image description here

当点击其中一个单元格并转到WebView时,表格视图单元格会因某种原因而改变高度。然后,当我从WebView返回到TableView时,我可以看到表格视图单元格更改高度

我认为这与细胞的高度错误有关,但我不确定。以下是我尝试过的内容:

1

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[D self]]) {
        return 490; // As of 11/13/14
    } else { // 2 other custom cells
        return tableView.rowHeight; // return the default height
    }
}

2

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[FR self]]) {
    ListTableViewCell *cellOne = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
    CGFloat heightOne = [cellOne.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightOne + 2;

    } else if ([model isKindOfClass:[D self]]) {
    ListTableViewCellTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
    CGFloat heightTwo = [cellTwo.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightTwo + 400;

    } else if ([model isKindOfClass:[YR self]]) {
    ListTableViewCellThree *cellThree = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
    CGFloat heightThree = [cellThree.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightThree + 2;

    } else {
        return 175;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[FR self]]) {

        FR *fD = (FR *)model;

        ListTableViewCell  *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];

        NSString *title = [NSString stringWithFormat:@"%@", fD.title];

        NSString *dateString = [self timeSincePublished:fD.pubDate];

        NSString *description = [self removeHTMLTags:fD.description];

        NSString *link = [NSString stringWithFormat:@"%@", fD.link];

        1Cell.labelHeadline.text = title;
        1Cell.labelDescription.text = description;
        1Cell.labelPublished.text = dateString;

                return 1Cell;

    } else if ([model isKindOfClass:[YR self]]) {

        YR *fD = (YR *)model;

        ListTableViewCell  *3Cell = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];

        NSString *title = [NSString stringWithFormat:@"%@", fD.title];

        NSString *dateString = [self timeSincePublished:fD.pubDate];

        NSString *description = [self removeHTMLTags:fD.description];

        NSString *link = [NSString stringWithFormat:@"%@", fD.link];

        3Cell.labelHeadline.text = title;
        3Cell.labelDescription.text = description;
        3Cell.labelPublished.text = dateString;

        return 3Cell;

            } else {

        D *dD = (D *)model;

        ListTableViewCellTwo *2Cell = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];

        NSString *dateString = [self timeSincePublished:dD.created_time];
        NSString *cap = [NSString stringWithFormat:@"%@", dD.cap.text];
        NSString *us = [NSString stringWithFormat:@"%@", dD.us.use];

        2Cell.labelHeadline.text = us;
        2Cell.labelDescription.text = cap;
        2Cell.labelPublished.text = dateString;

        return 2Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Remove grey background from Highlighted State
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

根据Matt Tang更新:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];


    if ([model isKindOfClass:[FR self]]) {


        ListTableViewCell *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
        if (!1Cell) {
            1Cell = [[ListTableViewCell alloc] init];

            FR *fD = (FR *)model;
            NSString *title = [NSString stringWithFormat:@"%@", fD.title];
            NSString *dateString = [self timeSincePublished:fD.pubDate];
            NSString *description = [self removeHTMLTags:fD.description];

            1Cell.labelHeadline.text = title;
            1Cell.labelDescription.text = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            1Cell.labelPublished.text = dateString;
        }
        CGFloat heightOne = [1Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        return heightOne + 2;


    } else if ([model isKindOfClass:[YR self]]) {


        ListTableViewCellThree  *3Cell = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
        if (!3Cell) {
            3Cell = [[ListTableViewCellThree alloc] init];

            YR *fD = (YR *)model;
            NSString *title = [NSString stringWithFormat:@"%@", fD.title];
            NSString *dateString = [self timeSincePublished:fD.pubDate];
            NSString *description = [self removeHTMLTags:fD.description];

            3Cell.labelHeadline.text = title;
            3Cell.labelDescription.text = description;
            3Cell.labelPublished.text = dateString;
        }
        CGFloat heightThree = [3Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        return heightThree + 2;


    } else if ([model isKindOfClass:[D self]]) {


        ListTableViewCellTwo *2Cell = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
        if (!2Cell) {
            2Cell = [[ListTableViewCellTwo alloc] init];

            D *dD = (D *)model;
            NSString *dateStringI = [self timeSincePublished:dD.created_time];
            NSString *TI = [NSString stringWithFormat:@"%@", dD.caption.text];
            NSString *us = [NSString stringWithFormat:@"%@", dD.us.use];

            2Cell.labelHeadline.text = us;
            2Cell.labelDescription.text = cap;
            2Cell.labelPublished.text = dateStringI;
        }
        CGFloat heightTwo = [2Cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        return heightTwo + 450;


    } else {

        return 490;
}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[FR self]]) {

        FR *fD = (FR *)model;

        ListTableViewCell  *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];

        if (!1Cell) {
            1Cell = [[ListTableViewCell alloc] init];

            FR *fD = (FR *)model;
            NSString *title = [NSString stringWithFormat:@"%@", fD.title];
            NSString *dateString = [self timeSincePublished:fD.pubDate];
            NSString *description = [self removeHTMLTags:fD.description];

            1Cell.labelHeadline.text = title;
            1Cell.labelDescription.text = description;
            1Cell.labelPublished.text = dateString;

        }

        return 1Cell;

    }

    // more code
    FR *fD = (FR *)model;

        ListTableViewCell  *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];

        NSString *title = [NSString stringWithFormat:@"%@", fD.title];

        NSString *dateString = [self timeSincePublished:fD.pubDate];

        NSString *description = [self removeHTMLTags:fD.description];

        NSString *link = [NSString stringWithFormat:@"%@", fD.link];

        1Cell.labelHeadline.text = title;
        1Cell.labelDescription.text = description;
        1Cell.labelPublished.text = dateString;

        return 1Cell;

}

CustomCell2.hCustomCell2.m现在没有任何内容):

@property (strong, nonatomic) IBOutlet UILabel *labelHeadline;
@property (strong, nonatomic) IBOutlet UILabel *labelPublished;
@property (strong, nonatomic) IBOutlet UILabel *labelDescription;
@property (strong, nonatomic) IBOutlet UIImageView *imageViewPic;

尝试设置断点。

1 个答案:

答案 0 :(得分:1)

你将不得不改变你实现tableView的方式:heightForRowAtIndexPath:

您调用dequeueReusableCellWithIdentifier:将您的自定义单元格对象返回以查找其高度。该方法仅跟踪屏幕上可见的单元格。当您离开该表视图时,它将自行清空。

您需要做的是检查dequeueReusableCellWithIdentifier后是否返回nil: * edit 你应该使用dequeueReusableCellWithIdentifier:forIndexPath:

如果它为零,则必须创建一个临时单元格,对其进行配置并返回其高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[FR self]]) {
        ListTableViewCell *cellOne = [tableView dequeueReusableCellWithIdentifier:@"1Cell" forIndexPath:indexPath];
        if (!cellOne) {
            cellOne = [[ListTableViewCell alloc] init];

            FR *fD = (FR *)model;
            NSString *title = [NSString stringWithFormat:@"%@", fD.title];
            NSString *dateString = [self timeSincePublished:fD.pubDate];
            NSString *description = [self removeHTMLTags:fD.description];
            NSString *link = [NSString stringWithFormat:@"%@", fD.link];

            cellOne.labelHeadline.text = title;
            cellOne.labelDescription.text = description;
            cellOne.labelPublished.text = dateString;
        }

        // Make sure the cell's frame is updated
        [cellOne setNeedsLayout];
        [cellOne layoutIfNeeded];

        CGFloat heightOne = [cellOne.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
        return heightOne + 2;

    } else if ([model isKindOfClass:[D self]]) {

    // more code
}

如果您想遵循DRY原则,请创建一个名为

的方法
- (ListTableViewCell *)configureCellForModel:(id)model;

并根据模型类中的所有自定义单元格配置代码。让它返回单元格并在tableView:cellForRowAtIndexPath:和tableView:heightForRowAtIndexPath:

中调用它

更新示例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[FR self]]) {

        FR *fD = (FR *)model;

        ListTableViewCell  *1Cell = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];

        if (!1Cell) {
            1Cell = [[ListTableViewCell alloc] init];

            FR *fD = (FR *)model;
            NSString *title = [NSString stringWithFormat:@"%@", fD.title];
            NSString *dateString = [self timeSincePublished:fD.pubDate];
            NSString *description = [self removeHTMLTags:fD.description];

            1Cell.labelHeadline.text = title;
            1Cell.labelDescription.text = description;
            1Cell.labelPublished.text = dateString;

        }

        return 1Cell;

    }

    // more code
}