0

I have a table with 2 types of prototype cells. First cell is acting as a static cell. And on `didselect' method of the other rows(from 1 to end), I just need to add a component to the 1st cell. And then the height of the 1st cell should be increased according to the content inside in it. My idea was trying to increase the cell height with,

- (CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath {
     if(indexPath.row == 0){

        TagTableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]];
        return [cell.tagView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height + 1;
     }else {
         return 75;
     }
}

But I couldn't access the cell. I got a bad request. Is this a convenient way to do this or is there any other ways? Advise me.

codebot
  • 2,540
  • 3
  • 38
  • 89
  • 1
    Try changing `[tableView cellForRowAtIndexPath:[NSIndexPath indexPathWithIndex:0]];` **to** `[tableView cellForRowAtIndexPath:indexPath];` **Reason:** indexPath.row condition is only for first row so we do not need to allocate a new instance of NSIndexPath. – Jugal Desai Jun 30 '16 at 22:05
  • You need to use `cell.contentView` instead of `cell.tagView`. – Jugal Desai Jun 30 '16 at 22:40

1 Answers1

0

Another simple way of implementing the above feature (uses a global NSString *firstRowText this helps to get the text from the variable anytime during run time rather than getting cell and then the text from it):

NSString *firstRowText = @"";

Height for cell:

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        if(indexPath.row==0) {
            return [self heightForTextViewRectWithWidth:tableView.frame.size.width andText:firstRowText];
        }
        return 20;
    }

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSIndexPath *firstCellIndex = [NSIndexPath indexPathForRow:0 inSection:0];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:firstCellIndex];
    firstRowText = [firstRowText stringByAppendingString:@"String to appened everytime on didSelectRow"];
    [cell.textLabel setText:firstRowText];

    [tableView reloadRowsAtIndexPaths:@[firstCellIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
}

Referenced from How to resize UITableViewCell to fit its content?

-(CGFloat)heightForTextViewRectWithWidth:(CGFloat)width andText:(NSString *)text
{
    UIFont * font = [UIFont systemFontOfSize:12.0f];

    // this returns us the size of the text for a rect but assumes 0, 0 origin
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: font}];

    // so we calculate the area
    CGFloat area = size.height * size.width;

    CGFloat buffer = 5.0f;

    // and then return the new height which is the area divided by the width
    // Basically area = h * w
    // area / width = h
    // for w we use the width of the actual text view
    return floor(area/width) + buffer;
}
Community
  • 1
  • 1
Jugal Desai
  • 164
  • 8