【问题标题】:iPhone - adjust size of table view according to textiPhone - 根据文本调整表格视图的大小
【发布时间】:2011-02-24 07:17:29
【问题描述】:

有没有办法调整表格视图的大小并让文本在 iPhone 中换行?我正在使用以下代码,它成功调整了单元格的高度,但是文本没有换行,它只是以“....”结尾

- (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;
}

【问题讨论】:

    标签: iphone tableview word-wrap


    【解决方案1】:

    这里有两件事:

    1. 在显示的标签中环绕文本 - 为此,您必须为 UILabel 定义它作为容器的外观 - 它可以有多少行,是否环绕,当然还有多大。您可以在代码中或在 Interface Builder 中执行此操作(取决于您是否使用自己的自定义单元格)

    2. 您可以在 Interface Builder 或代码中更改 UITableView 的宽度 (myTableView.frame = CGRectMake(x,y,width,height)) 但请注意,如果您使用 UITableViewController 作为视图控制器 - 您不能调整表格视图的宽度

    【讨论】:

      【解决方案2】:

      找到完美答案

      How do I wrap text in a UITableViewCell without a custom cell

      这是代码

      - (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;
      
      }
      

      【讨论】:

        【解决方案3】:

        在 UILabel 中分配你的字符串。然后使用这个Thread 来做。否则将字符串分配给 UItextView 并将其隐藏。您可以使用

         textview.contentSize.height//use this to your cell hieght
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-05
          • 1970-01-01
          • 1970-01-01
          • 2019-07-22
          相关资源
          最近更新 更多