【问题标题】:How to display multiple lines in a tableView cell如何在tableView单元格中显示多行
【发布时间】:2010-09-23 00:15:04
【问题描述】:

我有一个有点长的字符串,在 tablView 单元格中显示时会被截断。我这样做是为了使表格视图更宽:

tableView.rowHeight = 100;

如何使字体更小以及将文本换行在表格视图单元格中?

【问题讨论】:

    标签: objective-c cocoa-touch uitableview


    【解决方案1】:

    在tableView:cellForRowAtIndexPath: 中,您可以在 textLabel(或 descriptionLabel,取决于您使用的单元格样式)上设置一些属性来执行此操作。设置font 更改字体,设置linkBreakMode 使其自动换行,设置numberOfLines 设置最大行数(在此之后截断。您可以将其设置为0 表示没有最大值。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
        if( aCell == nil ) {
            aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
    
            aCell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
            aCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // Pre-iOS6 use UILineBreakModeWordWrap
            aCell.textLabel.numberOfLines = 2;  // 0 means no max.
        }
    
        // ... Your other cell setup stuff here
    
        return aCell;
    }
    

    【讨论】:

    • 据我所知,iOS6 中唯一不推荐使用的是UILineBreakModeWordWrap。 NSLineBreakByWordWrapping 相当于 iOS6。更新了我的答案。
    【解决方案2】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell* Cell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
        if( Cell == nil ) {
            Cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];
    
           Cell.textLabel.font = [UIFont fontWithName:@"TimesNewRoman" size:10.0];
           Cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
           Cell.textLabel.numberOfLines = 2;  // 0 means no max.
        }
    
      // your code here
    
        return Cell;
    }
    

    【讨论】:

      【解决方案3】:

      您应该继承 UITableViewCell 并使其具有 UITextView 而不是 UITextField。然后将您的子类分配给您的 UITableView。

      【讨论】:

        猜你喜欢
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多