首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UiTableView标签词包装

UiTableView标签词包装
EN

Stack Overflow用户
提问于 2015-05-31 09:52:39
回答 1查看 819关注 0票数 0

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

代码语言:javascript
复制
- (void)drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = {5, 10,5, 10};
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; 
}

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

代码语言:javascript
复制
- (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类有关吗?

编辑:

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

进口:

代码语言:javascript
复制
#import "ChatViewController.h"
#import "ChatTableViewCell.h"

@interface ChatViewController ()

@property (strong, nonatomic) ChatTableViewCell *customCell;

@end

下一步:

代码语言:javascript
复制
- (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:

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

发布于 2015-05-31 10:09:14

编辑

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

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

代码语言:javascript
复制
- (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*/;
}

别忘了

代码语言:javascript
复制
- (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;
}

希望这对你有帮助,快乐的编码..干杯!:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30556074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档