iPhone-根据文本调整表格视图的大小 [英] iPhone - adjust size of table view according to text

查看:68
本文介绍了iPhone-根据文本调整表格视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以调整表格视图的大小并将文本包装在iPhone中?我正在使用以下代码,它成功地调整了单元格的高度,但是文本没有自动换行,仅以"...."结束.

Is there a way to adjust the size of a table view AND get the text to wrap in iPhone? I'm using the following code, and it succeeds in adjusting the height of the cell, but the text doesn't wrap, it just ends with "...."

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

        City *thisCity = [cities objectAtIndex:indexPath.row];
        NSString * myString = thisCity.cityName;

        cellHeight = [myString sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(300.0, 1000.0) lineBreakMode:UILineBreakModeWordWrap];
        return cellHeight.height + 20;
}

推荐答案

在以下位置找到了完美的答案

Found the perfect answer at

如何包装文本在没有自定义单元格的UITableViewCell中

这是代码

- (UITableViewCell *)tableView:(UITableView *)tv
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{


    UITableViewCell *cell =
    [ tv dequeueReusableCellWithIdentifier:@"cell"];
    if( nil == cell ) {

        // this sets up a resusable table cell 
        cell = [ [[UITableViewCell alloc]
                  initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];

        // support line break mode
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

        // 0 means any number of lines
        cell.textLabel.numberOfLines = 0;

        // set it up with a consistent font with the height calculation (see below)
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];    

    }


    // set the name of the city 

    if (indexPath.row < cities.count ) {
        City *thisCity = [cities objectAtIndex:indexPath.row];
        cell.textLabel.text = thisCity.cityName;
        } 
        else 
        {

        cell.textLabel.text = @"Add New City...";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }


    return cell;

}

// get the height of the row

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

    City *thisCity = [cities objectAtIndex:indexPath.row];
    NSString * cellText = thisCity.cityName;

    // set a font size
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];

    // get a constraint size - not sure how it works 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

    // calculate a label size - takes parameters including the font, a constraint and a specification for line mode
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    // give it a little extra height
    return labelSize.height + 20;

}

这篇关于iPhone-根据文本调整表格视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆