动态uitableviewcell高度上的奇怪行为

时间:2015-03-04 06:53:10

标签: ios objective-c uitableview

我试图为我的客户细胞制作动态的uitableviewcell高度。

将单元格子类化以添加一些背景。

这是我的uitableview控制器类:

#define PADDING  23.0f

- (void)viewDidLoad
{

    [super viewDidLoad];

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger count = [self.entries count];

    return count + _rowcount;
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *reuseIdentifier = @"PlaceholderCell2";
    SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (sctvCell == nil) {


        sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];

    }
    NSUInteger nodeCount = [self.entries count];
    sctvCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    UILabel *label = (UILabel *)[sctvCell.contentView viewWithTag:1];
    if (nodeCount > 0)
    {


        NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];

        [label setText:appRecord.title];

        NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"B MyFont" size:14.0f]};

        CGRect rect = [appRecord.title boundingRectWithSize:CGSizeMake(label.frame.size.height - PADDING * 5, MAXFLOAT)
                                                    options:NSStringDrawingUsesLineFragmentOrigin
                                                 attributes:attributes
                                                    context:nil];
        CGRect newFrame = label.frame;
        newFrame.size.height = rect.size.height;

        label.frame = newFrame;
        [label sizeToFit];

        UIView *whiteRoundedCornerView = (UIView *)[sctvCell.contentView viewWithTag:1000];
        CGRect newFrame2 =  whiteRoundedCornerView.frame;
        newFrame2.size.width = 300;
        newFrame2.size.height = rect.size.height + 160;
        [ whiteRoundedCornerView setFrame:newFrame2];

    }

        if ((unsigned long)indexPath.row == [self.entries count] - 1){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
                       ^{
                           NewsFetchParseOperation *p = [[NewsFetchParseOperation alloc]init];
                           NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
                           p.cat = appRecord.Category;
                           self.intindex = self.intindex + 1;
                           p.index = [NSString stringWithFormat:@"%d", (int)self.intindex];
                           p.lastid = appRecord.ids;
                           [p main];
                           dispatch_async(dispatch_get_main_queue(), ^(void)
                                          {

                                               [self.tableView beginUpdates];
                                                   NSMutableArray *indexPaths = [NSMutableArray array];
                                                   NSInteger currentCount = self.entries.count;
                                                   for (NSUInteger i = 0; i < p.appRecordList.count; i++) {
                                                       [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
                                                   }
                                              NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
                                              self.entries = temp_1;
                                              [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
                                              [self.tableView endUpdates];

                                          });
                       });
    }
    return sctvCell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *reuseIdentifier = @"PlaceholderCell2";
    SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

    if (sctvCell == nil) {


        sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];

    }

        NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
        UILabel *label = (UILabel *)[sctvCell.contentView viewWithTag:1];
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"B MyFont" size:14.0f]};
        CGRect rect = [appRecord.title boundingRectWithSize:CGSizeMake(label.frame.size.height - PADDING * 5, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];


        return rect.size.height + PADDING * 6;

}

和我的细胞亚类:

- (void)awakeFromNib {
    self.news_img.layer.cornerRadius = 4;
    self.news_img.clipsToBounds = YES;
    self.resource_icon_img.layer.cornerRadius = 4;
    self.resource_icon_img.clipsToBounds = YES;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.whiteroundcorner = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,250)];
    self.whiteroundcorner.backgroundColor = [UIColor whiteColor];
    self.whiteroundcorner.layer.masksToBounds = NO;
    self.whiteroundcorner.layer.cornerRadius = 3.0;
    [self.whiteroundcorner.layer setShadowColor:[UIColor grayColor].CGColor];
    self.whiteroundcorner.layer.shadowOffset = CGSizeMake(-1, 1);
    self.whiteroundcorner.layer.shadowOpacity = 0.2;
    self.whiteroundcorner.tag = 1000;
    [self.contentView addSubview:self.whiteroundcorner];
    [self.contentView sendSubviewToBack:self.whiteroundcorner];
}
我正在为我的桌子使用故事板:

enter image description here

现在问题是大部分时间高度计算不正确。

也有一段时间高度超出细胞和10细胞末

当我尝试获取新行时,最后一个单元格错误地显示。

1 个答案:

答案 0 :(得分:0)

设置内容后,您需要计算单元格的高度。

这样的事情:

    - (CGFloat)heightForCellAtIndexPath:(NSIndexPath *)indexPath {

        static UITableViewCell *sizingCell = nil;
        static dispatch_once_t onceToken;

        dispatch_once(&onceToken, ^{

            sizingCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        });

/* Method where you set the content of the cell */ 

        [self configureCell: atIndexPath:indexPath];
        return [self calculateHeightForConfiguredSizingCell:sizingCell];
    }


    - (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {

        [sizingCell setNeedsDisplay];
        [sizingCell layoutIfNeeded];

        CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        return size.height;
    }