0

I am trying to add some text to my label. Once the text is bigger than the specified height of the cell. I want the cell to grow its hight as well. I did add the following code to take care of my label height, but I am not sure how to propagate that to my cell as well.

Code

- (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;
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
Jonathan
  • 2,728
  • 10
  • 43
  • 73
  • can you share more code, there are certain questions that need to be clarified, like do you update the cells runtime and reload the table view? or you have the reference of the label and you make the changes? – mangesh Mar 10 '13 at 05:16
  • @mangesh please have a look at the edit – Jonathan Mar 10 '13 at 05:21

2 Answers2

1

If you are using prototype cells, dequeueReusableCellWithIdentifier: returns a cell everytime provided the identifier is correct.

- (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;
    }
Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

You need to use delegate method heightForRowAtIndexPath for UITableView, here you could adjust height according to the text you have for the UITableViewCell You can follow the thread and it will help your for your case

Community
  • 1
  • 1
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
  • nsgulliver, I have extra labels added into the cell, so cell.textLabel doesn't seem to work for me – Jonathan Mar 10 '13 at 02:48
  • @Jonathan Since you have added scribbleBodyLabel, you need to follow the thread and resize the cell according to the that label. – Anupdas Mar 10 '13 at 04:25
  • @Anupdas can please elaborate on that? – Jonathan Mar 10 '13 at 04:28
  • @Jonathan Just to make sure before answering, now you can see the text in your cell correct. The thread nsgulliver shared just works. The idea is to set your numberOfLines to 0 which will resize automatically to contain the text added and heightForRowAtIndexPath: is used to calculate the height using sizeWithFont:constrainedToSize:, where you provide the text, the fontSize with which you want show, constrained to a size that is width is a fixed one and height can be any dimension. Just return the size after calculation plus add an offset to resize the cell to fit the label. – Anupdas Mar 10 '13 at 04:40
  • @Anupdas right now I can see just one like of my text. it does not automatically increase the size of the label or the cell. But when I scroll down and come back up in my view controller I can see the overflowing lines, but they come out of the cell. also I am not sure how to make heightForRowAtIndexPath account for my label's height. as this is an added label – Jonathan Mar 10 '13 at 05:00
  • you can reference the added labels (from their tags) and then look at their text and use that to account for the height. – Cashew Mar 10 '13 at 05:27
  • @Cashew can you please elaborate on that? – Jonathan Mar 10 '13 at 05:31
  • just check @Anupdas answer. Check the heightForRowAtIndexPath part and see how he accesses the labelBody and uses the text to determine the height constraint. – Cashew Mar 10 '13 at 05:36