Anatoliy Gatt
Anatoliy Gatt

Reputation: 2491

Resizing UITableView

I'm working on the project. Currently I'm facing a problem, I have 3 UITableView's in one ViewController. It looks something like this:

enter image description here

Now, The first TableView(top one), has just one row, that has a size of 55 pixels and it don;t have to be resizable. Next, second one is the TableView, again with one row, but this row must to resize depends on the content of it. Right now this TableView displaying this row:

enter image description here

This row must to resize depends on how long the text in it. What I did to calculate the size is:

if(tableView == self.usersPostOnTheWallTableView) {
    NSDictionary *propertyItems = [self.repliesItems objectAtIndex:indexPath.row];

    NSString *text = [self.postItems objectForKey:@"text"];

    NSLog(@"%@", text);

    CGSize constraintSize = CGSizeMake(280, MAXFLOAT);

    CGSize sizeText = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    self.height = MAX(sizeText.height, 44.0f);

    self.photosArray = [propertyItems objectForKey:@"attach_photos"];
    self.videosArray = [propertyItems objectForKey:@"attach_videos"];

    if((self.photosArray.count == 0) && (self.videosArray.count == 0)) {

        height += (CELL_CONTENT_MARGIN * 2);

    } else if ((self.videosArray.count > 0) && (self.photosArray.count == 0)) {

        height += 90 + (CELL_CONTENT_MARGIN * 2);

    } else if((self.photosArray.count > 0) && (self.videosArray.count == 0)) {

        height += 85 + (CELL_CONTENT_MARGIN * 2);
    }

}

Now, if the text is quite small, it fits the row well, and the row is resizes. Such as this:

enter image description here

But, after the text comes bigger, this what happening:

enter image description here

enter image description here

Obviously I don't need the scrolling right there, and I would disable it. But the thing here is that it takes the size of the text, and it resize the label, you can see how many space it make from the top and from the bottom of the label. But it don't put actual text there. Another thing is that it didn't resize the TableView it self, when I'm trying to resize it, I get the overlapping of the bottom table by the middle table.

Can anyone recommend any solution of this, or maybe another way of implementation?

Thanks in advance!

Upvotes: 0

Views: 331

Answers (1)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

U can do this is use tableviewDelegate method for setting cell height according your text like this for example:

This method will be called for every cell to decide how much cell should have height

  - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    NSString *strDetails = [yourTextArray objectAtIndex:indexPath.row];
   //if(indexPath.row == 1)
   //{
     CGSize stringSize = [strDetails sizeWithFont:[UIFont fontWithName:@"Marker Felt" size:13] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap]; // Provide text as well as font name in which your text will be displayed and constrainedToSize which defines width and height it should be maximum
     return stringSize.height+65; //Here 65 can be any value depending on Calculation  ImageView height + uppermost space + bottom space added
   //}
   //else 
   //{
   //  return 80 // anything u wishes
   //}
 }

Upvotes: 1

Related Questions