动态单元的高度

时间:2014-05-07 06:24:31

标签: ios dynamic uitableview height

我阅读了很多关于它的教程,但仍然无法做到正确。这是代码:

@implementation DCHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.tableViewContentArray = @[@"The franchise centers around a series of fantasy and science fantasy role-playing video games (RPGs), but includes motion pictures, anime, printed media, and other merchandise. The eponymous first game in the series, published in 1987",
                                   @"racing, third-person shooter, fighting, and rhythm.",
                                   @"e franchise. Recurring elements include plot themes, character names, and game mechanics. Plots center on a group of heroes battling a great evil while exploring the characters' internal struggles and relationships. Character names are frequently derived from the history, languages, and mythologies of cultures worldwide.",
                                   @"l Fantasy has been a driving force in the video game industry, and the series has affected Square Enix's business practices and its relationships with other video game developers. It has also introduced many features now common in role-playing video games and has been credited with helping to popularize console-based RPGs in markets outside Japan.",
                                   @" in Japan, has been bundled with Final Fantasy in several re-releases.[3][4][5] The last of the NES installments, Final Fantasy III, was release",
                                   @"he first six games to three-dimensional (3D) computer graphics; the game features polygonal characters on pre-rendered backgrounds. It also introduced a more modern setting, a style that was carried over to the next game.[3] It was also the first in the series to be released in Europe. The eighth installment was published in 1999, and was the first to consistently use realistically proportioned characters and feature a vocal piece as its theme music.[3][13] Final Fantasy IX, released in 2000, returned to the series' roots by revisiting a more traditional Final "];
}

#pragma mark - Table View methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }

    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    [cell.textLabel setLineBreakMode:NSLineBreakByCharWrapping];
    cell.textLabel.text = self.tableViewContentArray[indexPath.row];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font = [UIFont systemFontOfSize:14.0f];
    gettingSizeLabel.numberOfLines = 0;
    gettingSizeLabel.text = self.tableViewContentArray[indexPath.row];
    [gettingSizeLabel setLineBreakMode:NSLineBreakByCharWrapping];
    CGSize maximumLabelSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, CGFLOAT_MAX);

    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];


    CGFloat height = expectedSize.height;

    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableViewContentArray count];
}

@end

结果我的身高不正确,并且细胞的内容(如果它太长)被裁剪。 为什么会这样?

UPD:我也尝试使用NSString的bo​​undingRectWithSize方法,具有完全相同的属性:

NSDictionary *textAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],NSStrokeColorAttributeName: [UIColor grayColor]}. 

它根本不起作用 - 它只产生2行文字和非常大的宽度。看起来只是忽略了

CGSize templateSize = CGSizeMake(300.0f, CGFLOAT_MAX) that I have set.

2 个答案:

答案 0 :(得分:1)

修复了这个问题:

CGSize maximumLabelSize = CGSizeMake(290.0f, CGFLOAT_MAX);

CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

CGFloat height = expectedSize.height + 1;

290,因为cell.textLabel的宽度。但!身高+ 1是某种即兴表演。所以我仍然不知道我为什么要制作这个" + 1",但它确实有效。 有人知道这是什么意思,为什么不包含在sizeThatFits返回值中?

答案 1 :(得分:0)

试试这个......

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

        float hh=20;

        if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
            CGSize maximumLabelSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, FLT_MAX);
            CGSize expectedLabelSize = [[self.tableViewContentArray objectAtIndex:indexPath.row] sizeWithFont: [UIFont systemFontOfSize:14.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByCharWrapping];
            NSLog(@"%f",expectedLabelSize.height);
            hh+=expectedLabelSize.height;
        } else {
            // code here for iOS 7.0

            CGSize constrainedSize = CGSizeMake(self.myTableView.frame.size.width - 20.0f, FLT_MAX);

            NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                              [UIFont systemFontOfSize:14.0f], NSFontAttributeName,
                                              nil];

            NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[self.tableViewContentArray objectAtIndex:indexPath.row] attributes:attributesDictionary];

            CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
            NSLog(@"%f",requiredHeight.size.height);
            hh+=requiredHeight.size.height;

        }

        return hh;
    }