objective-c - 使所有文本显示在 UITableView 单元格中

标签 objective-c ios xcode uitableview

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

enter image description here

我希望看到文本到达单元格末尾,然后另起一行。

我的代码现在看起来像这样。

@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) 的高度应该可以正常工作。

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

谢谢。

最佳答案

您将需要使用以下方法

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

关于objective-c - 使所有文本显示在 UITableView 单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11874323/

相关文章:

objective-c - 使用未声明的标识符 'NSEntityDescription'

ios - 如何回到 Storyboard 中的 Root View Controller ?

ios - 在哪里可以找到 iOS8 蓝牙的文档

php - 转换 NSArray -> JSON -> NSData -> PHP 服务器 ->JSON 表示

ios - 金刚鹦鹉 iOS - 点击动画或反转

SwiftUI 更新导航操作的状态

ios - 尽可能简单地发送 iMessage iOS

ios - 从 UITableView 中的 UI 中删除标题和相应的单元格

iphone - 应用提交,图标大小错误由于即将发布的IOS7

ios - fatal error : Array index out of range with Parse. com