【问题标题】:Making all the text show in a UITableView Cell使所有文本显示在 UITableView 单元格中
【发布时间】:2016-10-10 22:51:46
【问题描述】:

我正在将一个数组加载到 UITableView 中,并且该数组的组件很长。我想要做的是自定义单元格,以便它们的长度将调整以适应文本,但宽度将保持不变。现在正在发生的事情是当文本变得太长时,它只会注销单元格,您会看到这一点。

我希望看到文本到达单元格的末尾,然后开始新的一行。

我现在的代码如下所示。

@implementation CRHCommentSection
@synthesize observationTable; 

NSArray *myArray; 



- (void)viewDidLoad
{


    myArray = [CRHViewControllerScript theArray];  
    NSLog(@"%@", myArray);
    //NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0   inSection:1]];
   //[observationTable insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];

   [observationTable reloadData]; 


    [super viewDidLoad];
// Do any additional setup after loading the view.

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.

    NSLog(@" in method 1");
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 NSLog(@" in method 2");
// Return the number of rows in the section.
return [myArray count];


}

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

NSLog(@" in method 3");

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
//cell.textLabel.text = @"Label"; 


return cell;
}

我还发现了一些其他人为多行单元格编写的代码,但不知道如何根据数组中字符串的长度使其自动运行。

static NSString *CellIdentifier = @"MyCell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Label';
cell.detailTextLabel.text = @"Multi-Line\nText";
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;

他们说您还需要为多行单元格返回合适的高度,并且 (44.0 + (numberOfLines - 1) * 19.0) 的高度应该可以正常工作。

有没有人知道如何做到这一点?

谢谢。

【问题讨论】:

    标签: objective-c ios xcode uitableview


    【解决方案1】:

    您将需要使用以下方法

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // this method is called for each cell and returns height
        NSString * text = @"Your very long text";
        CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize: 14.0] forWidth:[tableView frame].size.width - 40.0 lineBreakMode:NSLineBreakByWordWrapping];
        // return either default height or height to fit the text
        return textSize.height < 44.0 ? 44.0 : textSize.height;
    }
    
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString * cellIdentifier = @"YourTableCell";
    
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
                                          reuseIdentifier:cellIdentifier];
    
            [[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
            [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
            [[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
        }
    
        [[cell textLabel] setText:@"Your very long text"];
    
        return cell;
    }
    

    【讨论】:

    • 但是如何让实际单元格改变其高度是另一个问题。
    • 想通了。我通过执行 int numberOfLines = ([[myArray objectAtIndex:indexPath.row] length] / 44); 为每个单元格的总行数做了一个 int。我除以 44,因为这是基于单元格宽度的一行上的最大字符数。然后我返回了这个: return textSize.height = (44.0 + ( numberOfLines - 1) * 19.0);它根据文本的长度为每个单元格提供了合适的单元格大小。
    • 对我不起作用,CGSize 由于某种原因返回错误的高度。
    • @BorisGafurov 没有看到你的任何代码很难帮助你,创建新问题。
    • @Craig numberOfLines = 0,也可以。那么你就不必计算线了。
    【解决方案2】:

    你要看看NSString函数-(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size

    您必须将其包含在您的heightForRowAtIndexPath 函数中以确定正确的高度。

    另外,将标签的 numberOfLines 设置为 0。这允许它调整到所需的任何行数。

    【讨论】:

      【解决方案3】:

      如何在表格视图单元格中使用 UITextView。

      首先,您必须更改单元格的高度。

      接下来,将 UITextView 放入单元格中。

      当然,您可以通过代码而不是 IB 来做到这一点。

      【讨论】:

      • 如何将文本视图添加到数组中每个组件的单元格中?在我的情况下,这个数字可能会有所不同。
      【解决方案4】:

      对我来说,接受的答案是通过添加 else 条件来实现的。 在我的情况下,如果条件没有得到满足,因为[tableView dequeueReusableCellWithIdentifier:@"comment"]; 总是返回不为零的单元格。

          - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      
          NSInteger item = indexPath.item;
          UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment"];
      
          if (cell == nil)
          {
              cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                            reuseIdentifier:@"comment"];
      
              [[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
              [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
              [[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
          }
           // added else condition
          else {
              [[cell textLabel] setNumberOfLines:0];
              [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
              [[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
          }
      
          [[cell textLabel] setText:commentsText[item]];
      
          return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多