【问题标题】:UITableViewCell not resizing with UILabelUITableViewCell 不使用 UILabel 调整大小
【发布时间】:2015-02-18 13:32:18
【问题描述】:

我使用 parse 来存储用户 cmets,并在 UITableViewController 中显示这些。我正在尝试使 UITableViewCell 动态化,以便单元格随 UILabel 的大小而变化。我得到了奇怪的结果。

我的commentTableViewController.m

    #import "CommentsTableViewController.h"

#import <Parse/Parse.h>

#import "AddCommentViewController.h"

#import "CustomTableViewCell.h"



@interface CommentsTableViewController ()



@property (strong, nonatomic) CustomTableViewCell *customCell;



@end

@implementation CommentsTableViewController



-(void)viewDidLoad

{

    [self getWallImages];

}



-(void)viewDidAppear:(BOOL)animated

{

    [self getWallImages];

    [self.tableView reloadData];

}



-(void) viewWillAppear: (BOOL) animated

{

    [self getWallImages];

    [self.tableView reloadData];

}



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

{

    return self.commentsArray.count;

}



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

{

    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];



    PFObject *commentObject = [self.commentsArray objectAtIndex:indexPath.row];

    cell.userLabel.text = [commentObject objectForKey:@"user"];

    cell.commentLabel.text = [commentObject objectForKey:@"comment”];



        return cell;

}



- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return self.titleNameString;

}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

     if(!self.customCell) {

    self.customCell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];

    }

    PFObject *commentObject = [self.commentsArray objectAtIndex:indexPath.row];



    self.customCell.userLabel.text = [commentObject objectForKey:@"user"];

    self.customCell.commentLabel.text = [commentObject objectForKey:@"comment"];



    self.customCell.commentLabel.preferredMaxLayoutWidth = self.tableView.bounds.size.width;



    [self.customCell layoutIfNeeded];





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



    return height;

 }

-(void)getWallImages

{

    PFQuery *query = [PFQuery queryWithClassName:[NSString stringWithFormat:@"%@",self.titleNameString]];

    [query orderByDescending:@"createdAt"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {



        if (!error) {



            self.commentsArray = nil;



            self.commentsArray = [[NSArray alloc] initWithArray:objects];



            [self.tableView reloadData];



        } else {



            NSString *errorString = [[error userInfo] objectForKey:@"error"];



            UIAlertView *errorAlertView = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];



            [errorAlertView show];

        }

    }];

}

不确定这是否与故事板中我的 UITableViewCell 的设置或我的 UILabel 约束有关......

如果我将 commentLabel 的优先级约束设置为小于 500(例如 250),我会得到这个(标签覆盖所有单元格并且通常不起作用)

如果优先级大于 500,则单元格中不会显示任何内容。

【问题讨论】:

  • 能否也显示自动布局代码?
  • 我在哪里可以找到它?我只在故事板中使用过它
  • 也许你可以在你的故事板中制作一个约束的截图?
  • 添加了约束的屏幕截图

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

嗯,我有一个类似的要求,我构建它的方式是我将标签约束设置到它的所有边缘到超级视图,即 contentView,但没有指定宽度或高度约束。

然后我刚刚计算了单元格对于给定文本所需的高度,并将 heightForRowAtIndexPath 设置为计算出的高度。

因此,当单元格展开时,它也会拉动标签以适应其高度。

这是计算高度的函数的 sn-p。

   -(CGSize)heightforCellForText:(NSString*)text font:(UIFont*)font width:(CGFloat)width
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, CGFLOAT_MAX)];
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.font = font;
    label.text = text;
    [label sizeToFit];
    return label.frame.size;
}

我使这个方法更加动态以适应字体的变化。所以只需将您的标签字体作为参数传递。

干杯!

【讨论】:

    【解决方案2】:

    你需要实现:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        //CALCULATE HEIGHT HERE
    
    }
    

    这是委托方法之一,应该可以解决问题。您可以通过查看 UILabel 中有多少个字符来计算高度。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //get the cell
        CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    
    //say we know that there are 15 characters  per line
    
    int chars = cell.commentLabel.text.length;
    int lines = chars/15;
    
    //say our default cell height is 50, maybe we want 5 pts per line
    
    float height = 50 +5*lines;
    
    return height;
    
    
    
    
    
    }
    

    【讨论】:

    • 我已经有了 heightForRowAtIndexPath 委托方法,但它似乎不起作用。你能看看我这个方法的代码,看看我哪里出错了吗?
    • 好吧,首先要尝试的是注释掉该方法中的所有代码并手动设置高度,例如200,看看它是否改变了单元格的高度。那么我们可以确定是不是计算导致了问题
    • 那么我要做的就是编写一些代码来计算文本字段中的行数并从中产生一个高度。我会写一个例子..
    • 看看我写的,它的代码并不完美,但希望你能明白。
    • 是的,我明白你在这里做什么,它不喜欢 CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];由于某种原因,它在碰到它时会不断崩溃
    猜你喜欢
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    相关资源
    最近更新 更多