且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

标签在UITableViewCell中对齐

更新时间:2023-12-04 13:32:34

这是另一种没有子类化单元格的解决方案,所以它肯定适用于不同的表格样式。 (我没有检查过其他解决方案。)标题和详细信息字符串在我的UITableViewController标头中声明并已定义。它们不是很长,当然在4000高度内!

Here is another solution without subclassing cells, so it certainly works with different table styles. (I haven't checked other solutions.) The title and detail strings are declared in my UITableViewController header and already defined. They aren''t very long, certainly well within height 4000!

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

    CGRect frame = [UIScreen mainScreen].bounds;
    CGFloat width = frame.size.width;

    int section = indexPath.section;
    int row = indexPath.row;

    NSString *title_string = [title_strings_array objectAtIndex:row];
    NSString *detail_string = [detail_strings_array objectAtIndex:row];

    CGSize title_size = {0, 0};
    CGSize detail_size = {0, 0};

    if (title_string && [title_string isEqualToString:@""] == NO ) {
        title_size = [title_string sizeWithFont:[UIFont systemFontOfSize:22.0]
                              constrainedToSize:CGSizeMake(width, 4000)
                                  lineBreakMode:UILineBreakModeWordWrap];
    }

    if (detail_string && [title_string isEqualToString:@""] == NO ) {
        detail_size = [detail_string sizeWithFont:[UIFont systemFontOfSize:18.0]
                                constrainedToSize:CGSizeMake(width, 4000)
                                    lineBreakMode:UILineBreakModeWordWrap];
    }

    CGFloat title_height = title_size.height;
    CGFloat detail_height = detail_size.height;

    CGFloat content_size = title_height + detail_height;

    CGFloat height;

    switch ( section ) {

        case 0:
            height = content_size;
            break;

        //Just in case  
        default:
            height = 44.0;
            break;

    }

    return height;

}