丑陋的heightForRowAtIndexPath和cellForRowAtIndexPath

时间:2013-02-15 06:19:29

标签: iphone ios objective-c xcode uitableview

我有一个UItableview,我使用heightForRowAtIndexPathcellForRowAtIndexPath填充了数据。显然Apple让我在我的代码中做两次事情。

首先,我必须在heightForRowAtIndexPath中计算我的观点的大小(因为我必须制作它们),然后我必须再次制作它们,将它们添加到实际视图中。

我有一个非常复杂的视图,所以当你必须写两次时​​它看起来很难看。

有没有更好的方法来做到这一点?

更新

这是我的代码的样子。它不完全一样,但非常接近。为什么苹果让我写这两次?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"heightForRowAtIndexPath");

    //Initiating strings
    NSString *headlineString;
    NSString *subHeadlineString;
    NSString *bylineString;
    if (global.magazine.issues.count==0) {
        return 45;
    }else if(indexPath.section == global.magazine.issues.count+1) {
        //Finding the right issue and article for this row
        Issue *issue = [global.magazine.issues objectAtIndex:global.magazine.issues.count-1];

        //Creating the headline
        headlineString = [NSString stringWithFormat:@"<span class='bold_style'>FOREWORD</span>"];

        //Creating the subHeadline
        subHeadlineString = [NSString stringWithFormat:@"%@", [issue.magazine_foreword substringToIndex:100]];

        //Creating byline
        bylineString = [[NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", issue.magazine_byline] capitalizedString];
    }else{
        //Finding the right issue and article for this row
        Issue *issue = [global.magazine.issues objectAtIndex:indexPath.section-1];
        Article *article = [issue.articles objectAtIndex:indexPath.row];

        //Creating the headline
        headlineString = [NSString stringWithFormat:@"<span class='bold_style'>%@</span>", [article.title uppercaseString]];

        //Creating the subHeadline
        subHeadlineString = [NSString stringWithFormat:@"%@", [article.main_text substringToIndex:100]];

        //Creating byline
        bylineString = [NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", article.byline];
    }

    //Creating the labels
    NMCustomLabel *headline = [global.label headLineLabelWithString:headlineString fromTop:30 withWidth:global.screenWidth-60];
    NMCustomLabel *subHeadline = [global.label subHeadlineLabelWithString:subHeadlineString fromTop:30+headline.height+10 withWidth:global.screenWidth-60];
    NMCustomLabel *byline = [global.label articleBylineLabelWithString:bylineString fromTop:30+headline.height+10+subHeadline.height+10 withWidth:global.screenWidth-60];

    //Setting the height of the row
    return 30+headline.height+10+subHeadline.height+10+byline.height+30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"cellForRowAtIndexPath");
    //Preparing the cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }

    //Removing former text views
    for (UIView *subview in [cell subviews]) {
        if (subview.tag == 21 || subview.tag == 22 || subview.tag == 23) [subview removeFromSuperview];
    }

    //Removing and setting tableview border
    [[cell viewWithTag:30] removeFromSuperview];
    UIView *rightBorder = [[UIView alloc] initWithFrame:CGRectMake(cell.width-1, 0, 1, cell.height)];
    rightBorder.backgroundColor = global.lightGrey;
    rightBorder.tag = 30;
    [cell addSubview:rightBorder];

    //Setting the seletion background color on the cells
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = global.extraLightGrey;
    cell.selectedBackgroundView = bgColorView;
    if (global.magazine.issues.count==0) {
        return cell;
    }else if (indexPath.section-1 == global.magazine.issues.count) {
        //Finding the right issue and article for this row
        Issue *issue = [global.magazine.issues objectAtIndex:global.magazine.issues.count-1];

        //Creating the headline
        NSString *headlineString = [NSString stringWithFormat:@"<span class='bold_style'>FOREWORD</span>"];
        NMCustomLabel *headline = [global.label headLineLabelWithString:headlineString fromTop:30 withWidth:global.screenWidth-60];
        headline.tag = 21;
        [cell addSubview:headline];

        //Creating the subHeadline
        NSString *subHeadlineString = [[NSString stringWithFormat:@"%@", issue.magazine_foreword] substringToIndex:100];
        NMCustomLabel *subHeadline = [global.label subHeadlineLabelWithString:subHeadlineString fromTop:30+headline.height+10 withWidth:global.screenWidth-60];
        subHeadline.tag = 22;
        [cell addSubview:subHeadline];

        //Creating byline
        NSString *bylineString = [[NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", issue.magazine_byline] capitalizedString];
        NMCustomLabel *byline = [global.label articleBylineLabelWithString:bylineString fromTop:30+headline.height+10+subHeadline.height+10 withWidth:global.screenWidth-60];
        byline.tag = 23;
        [cell addSubview:byline];
    }else{
        //Finding the right issue and article for this row
        Issue *issue = [global.magazine.issues objectAtIndex:indexPath.section-1];
        Article *article = [issue.articles objectAtIndex:indexPath.row];

        //Creating the headline
        NSString *headlineString = [NSString stringWithFormat:@"<span class='bold_style'>%@</span>", [article.title uppercaseString]];
        NMCustomLabel *headline = [global.label headLineLabelWithString:headlineString fromTop:30 withWidth:global.screenWidth-60];
        headline.tag = 21;
        [cell addSubview:headline];

        //Creating the subHeadline
        NSString *subHeadlineString = [NSString stringWithFormat:@"%@", [article.main_text substringToIndex:100]];
        NMCustomLabel *subHeadline = [global.label subHeadlineLabelWithString:subHeadlineString fromTop:30+headline.height+10 withWidth:global.screenWidth-60];
        subHeadline.tag = 22;
        [cell addSubview:subHeadline];

        //Creating byline
        NSString *bylineString = [[NSString stringWithFormat:@"<span class='ital_style'>By %@</span>", article.byline] capitalizedString];
        NMCustomLabel *byline = [global.label articleBylineLabelWithString:bylineString fromTop:30+headline.height+10+subHeadline.height+10 withWidth:global.screenWidth-60];
        byline.tag = 23;
        [cell addSubview:byline];
    }

    return cell;
}

4 个答案:

答案 0 :(得分:2)

最简单的解决方案是遵循DRY原则,并将高度添加为您用作数据源的对象的属性,或者向视图控制器添加方法,例如:

-(CGFloat)calculateHeightForHeadline:(NSString*)headline andSubHeadline:(NSString*)subHeadline andByLine:(NSString*)byLine

然后至少你只在一个地方有计算代码。

或者,您可以从您的cellForRowAtIndexPath方法

中调用[tableView heightForRowAtIndexPath:indexPath]

答案 1 :(得分:2)

你必须做两次因为表视图在绘制任何东西之前需要知道它的总高度 - 所以它在调用任何单元格方法之前调用每行的height方法。根据您当前的代码,根据行数,您可能会在表格出现之前稍微延迟 - 乐器会告诉您这是您花费时间的高度方法。

我不知道您自定义标签类的作用,但您可以通过使用字符串或属性字符串绘制和大小计算UIKit extensions来计算高度,而无需创建视图(这是昂贵的),这是为了这个目的而创建的。

答案 2 :(得分:0)

heightForRowAtIndexPath

将为您的所有行调用。此委托方法返回行的高度。由于您的设计需要根据条件不同的高度。不幸的是,每次创建行之前,您都必须计算行高。这就像我们在绘制行之前决定什么是它的高度。

有一种方法可以避免这种情况。但我不建议你去做。因为它会改变UITableView的设计。你可以做的是你可以从你拥有的所有条件中决定你行的最大高度。例如。我们将其视为100像素。然后你可以绘制其余的细胞。但是,如果您的行中的任何行小于100像素,则会留下空白区域。它看起来很毛茸茸。

基本上,为了满足您的要求,您必须两次这样做。别无选择: - (

答案 3 :(得分:0)

我想知道,为什么要在heightForRowAtIndexpath中创建自定义标签?为什么不用sizeWithFont:或这样的方法计算文本的大小?我认为这是计算行高的更好方法。祝你好运!