【问题标题】:UiTableView label word wrapUiTableView 标签自动换行
【发布时间】:2015-05-31 09:52:39
【问题描述】:

我用一个单独的 UILabel 类定义单元格中我的标签的边距:

- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {5, 10,5, 10};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; 
}

使用以下代码,我定义了单元格的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.customCell) {
        self.customCell = [self.chatTableView dequeueReusableCellWithIdentifier:@"CellChat"];
    }

    self.customCell.sender.text = [array objectAtIndex:indexPath.row];

    [self.customCell layoutIfNeeded];

    CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    return height +1; 
}

我的问题:当我测试应用程序时,它不会在正确的位置进行自动换行。为什么?和 UILabel 类有关系吗?

编辑:

谢谢,但我还是不行。我没有错误,但是当我加载表格视图时,它不显示文本。目前,我的代码如下所示:

进口:

#import "ChatViewController.h"
#import "ChatTableViewCell.h"

@interface ChatViewController ()

@property (strong, nonatomic) ChatTableViewCell *customCell;

@end

下一个:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UILabel *gettingSizeLabel = [[UILabel alloc] init];
    gettingSizeLabel.font = [UIFont fontWithName:@"Arial" size:15];
    PFObject *tempObject = [array objectAtIndex:indexPath.row];
    gettingSizeLabel.text = [tempObject objectForKey:@"Sender"];
    gettingSizeLabel.numberOfLines = 0;

    CGSize maximumLabelSize = CGSizeMake(140, MAXFLOAT);
    CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];

    return expectedSize.height + (10*2);
}

我的 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifer = @"CellChat";

    ChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer];

    PFObject *tempObject = [array objectAtIndex:indexPath.row];
    cell.sender.text = [tempObject objectForKey:@"Sender"];
    cell.receptor.text = [tempObject objectForKey:@"Receptor"];
    cell.sender.lineBreakMode = NSLineBreakByWordWrapping;
    cell.sender.numberOfLines = 0;

    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview uilabel


    【解决方案1】:

    编辑

    不要在- (CGFloat)tableView:tableView heightForRowAtIndexPath:indexPath 中使用self.customCell

    相反,计算单元格高度如下:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        UILabel *gettingSizeLabel = [[UILabel alloc] init];
        gettingSizeLabel.font = font;
        gettingSizeLabel.text = [array objectAtIndex:indexPath.row];  // your text inside [array objectAtIndex:indexPath.row];
        gettingSizeLabel.numberOfLines = 0;
    
        CGSize maximumLabelSize = CGSizeMake(yourMaxWidth, MAXFLOAT);
    
        CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
    
        return expectedSize.height /*then add your margin 'y' margin multiplied by two for the top and bottom margin*/;
    }
    

    别忘了设置

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellID = @"CellChat";
    
        self.customCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
        if (!self.customCell)
        {
            self.customCell = [[YourCellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
    
        // dont forget to set this
    
        self.customCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        self.customCell.textLabel.numberOfLines = 0;
        // [self.customCell sizeToFit]; optional depending from what you want
        return self.customCell;
    }
    

    希望这对您有所帮助,编码愉快.. 干杯! :)

    【讨论】:

    • 是正确的。调用 heightForRowAtIndexPath 时,您的单元格尚未初始化。它的第一个 heightForRowAtIndexPath 将被调用,然后 cellForRowAtIndexPath。
    猜你喜欢
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多