iphone - 动态改变原型(prototype)单元的高度

标签 iphone ios objective-c

我正在尝试向标签添加一些文本。一旦文本大于单元格的指定高度。我希望细胞也能长到最高。我确实添加了以下代码来处理我的标签高度,但我不确定如何将其传播到我的单元格。

代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"Scribbles";
    //Array
    Scribble *scribble1 = [Scribble new];
    [scribble1 setTitle:@"Test 1" andBody:@"This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun! This is fun!" andImage:@"scribble.png"];
    Scribble *scribble2 = [Scribble new];
    [scribble2 setTitle:@"Test 2" andBody:@"This is fun!" andImage:@"scribble2.png"];
    Scribble *scribble3 = [Scribble new];
    [scribble3 setTitle:@"Test 3" andBody:@"This is fun!" andImage:@"scribble3.png"];
    Scribble *scribble4 = [Scribble new];
    [scribble4 setTitle:@"Test 4" andBody:@"This is fun!" andImage:@"scribble.png"];
    Scribble *scribble5 = [Scribble new];
    [scribble5 setTitle:@"Test 5" andBody:@"This is fun!" andImage:@"scribble2.png"];
    Scribble *scribble6 = [Scribble new];
    [scribble6 setTitle:@"Test 6" andBody:@"This is fun!" andImage:@"scribble3.png"];
    Scribble *scribble7 = [Scribble new];
    [scribble7 setTitle:@"Test 7" andBody:@"This is fun!" andImage:@"scribble.png"];
    Scribble *scribble8 = [Scribble new];
    [scribble8 setTitle:@"Test 8" andBody:@"This is fun!" andImage:@"scribble3.png"];

    scribbles = [NSArray arrayWithObjects:
                 scribble1,scribble2,scribble3,scribble4,scribble5,scribble6,scribble7,scribble8, nil];
}

//To make the Selection disappear
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];

}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return scribbles.count;
}

//Loading stuff into tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"ScribbleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    Scribble *scribble = [scribbles objectAtIndex:indexPath.row];

    UIImageView *scribbleImageView = (UIImageView *)[cell viewWithTag:100];
    scribbleImageView.image = [UIImage imageNamed:scribble.image];
    scribbleImageView.layer.cornerRadius = 18.0;
    scribbleImageView.clipsToBounds = YES;

    UILabel *scribbleNameLabel = (UILabel *)[cell viewWithTag:101];
    scribbleNameLabel.text = scribble.title;

    UILabel *scribbleBodyLabel = (UILabel *)[cell viewWithTag:102];
    [scribbleBodyLabel setLineBreakMode:NSLineBreakByWordWrapping];
    scribbleBodyLabel.numberOfLines = 0;
    scribbleBodyLabel.text = scribble.body;
    [scribbleBodyLabel sizeToFit];
    return cell;
}

最佳答案

如果您使用原型(prototype)单元格,则 dequeueReusableCellWithIdentifier: 只要标识符正确,每次都会返回一个单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"ScribbleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    Scribble *scribble = scribble[indexPath.row];

    UIImageView *scribbleImageView = (UIImageView *)[cell viewWithTag:100];
    scribbleImageView.image = [UIImage imageNamed:scribble.image];
    scribbleImageView.layer.cornerRadius = 18.0;
    scribbleImageView.clipsToBounds = YES;

    UILabel *scribbleNameLabel = (UILabel *)[cell viewWithTag:101];
    scribbleNameLabel.text = scribble.title;

    UILabel *scribbleBodyLabel = (UILabel *)[cell viewWithTag:102];
    [scribbleBodyLabel setLineBreakMode:NSLineBreakByWordWrapping];
    scribbleBodyLabel.numberOfLines = 0;
    scribbleBodyLabel.text = scribble.body;
    return cell;
}

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        Scribble *scribble = scribble[indexPath.row];

        NSString *scribbleBody = scribble.body;
        UIFont *cellFont = [UIFont systemFontOfSize:17.0f];
        /*Instead of hardcoded width you can find width from tableView frame 
        imageView and their offsets 20 for tableView ,40 for imageView, 30 for 
        offset of body label  */
        CGFloat width = tableView.frame.size.width - 90.0f;
        CGSize constraintSize = CGSizeMake(width, MAXFLOAT);
        CGSize labelSize = [scribbleBody sizeWithFont:cellFont
                                    constrainedToSize:constraintSize
                                        lineBreakMode:NSLineBreakByWordWrapping];

        //Add the height of scribbleNameLabel +offset
        return labelSize.height + 40.0f;
    }

关于iphone - 动态改变原型(prototype)单元的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318235/

相关文章:

ios - 如何使 AVPlayer 随父 View 调整大小?

iphone - 如何实现像evernote-iphone一样的编辑

iphone - 在模拟器中启动 iOS 应用程序时出现白色 View

ios - viewDidLoad 之后如何创建/添加 UIView?

iphone - 使用 CSS 修复区域 map 以使其在 iPhone 和小型设备上正确显示?

ios - swift - 跳跃时暂停动画

ios - iPad Retina 模型快速识别

iOS UISearchController 崩溃 : Application tried to present modal view controller on itself

objective-c - 在 XCode 中更改 NSImage - 这行代码不起作用

ios - 绕圈旋转