2
votes

There is a NSTextField in a NSTableView.

The height of the text in the window resizing is increased. but Not grow the cells in a table view.

In this case, the height of table view cell to alter how do you like?

in short, When resizing the size of the text view, I want to change the size of the cell.

like this: (Mac store) enter image description here

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row;
{
    KFCustomCellView *cellView = [tableView makeViewWithIdentifier:@"MainCell" owner:self];

    NSString *comment = [[_commentList objectAtIndex:row]valueForKey:@"COMMENT"];

    [cellView.comment setStringValue:comment];

    float cellheight = [comment heightForStringDrawing:comment font:[cellView.comment font] width:self.view.frame.size.width * 0.8];

    if (cellheight > cellView.comment.frame.size.height)
    {
        return (cellheight + 65);
    }

    return  90;
}
2

2 Answers

0
votes

Use following approach the to resize cell height according to textview content :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *DataCellId = @"DataCell";
    dataCell = (Cell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
    if(!dataCell)
    {
        dataCell = [[[Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId] autorelease];
    }

    dataCell.dataCellTextView.tag = indexPath.row;
    dataCell.dataCellTextView.text =[yourDataArry objectAtIndex:indexPath.row];
return dataCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    #define PaddingForTableRowHeight 24

    NSString *text;
    if([yourDataArry count] > indexPath.row)
    {
        text = [yourDataArry objectAtIndex:indexPath.row];
    } else {
        text = @"";
    }

     UIFont *dataFont = [UIFont boldSystemFontOfSize:16.0f];

     CGSize textSize ;

     if([UICommonUtils checkIfiOS7])
        {
        // For iOS7
            textSize = [text boundingRectWithSize:CGSizeMake("YOUR CELL WIDTH", MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:dataFont} context:nil].size;
        } else {
            textSize = [text sizeWithFont:dataFont constrainedToSize:CGSizeMake("YOUR CELL WIDTH" - PaddingForTableRowHeight,MAXFLOAT)];
        }


     float textViewHeight =  MAX(kDefaultCellHeight, textSize.height+PaddingForTableRowHeight);

  return textViewHeight;
}


-(void)textViewDidBeginEditing:(UITextView *)textView
{
  [self scrollToCursorForTextView:textView];
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    if([yourDataArry count] > textView.tag)
    {
        [yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
    }
}

- (void)textViewDidChange:(UITextView *)textView
{
    dataUpdated =YES;
    if([yourDataArry count] > textView.tag)
    {
        [yourDataArry replaceObjectAtIndex:textView.tag withObject:textView.text];
    }
    [self performSelector:@selector(dataReload) withObject:nil afterDelay:0.0];
    [self scrollToCursorForTextView:textView];
}

-(void)dataReload
{
    [self.tbl beginUpdates];
    [self.tbl endUpdates];
}
0
votes

There are two methods which are used to draw a cell if you know 1-viewForTableColumn 2-objectValueForTableColumn In the above methods,when you draw your textfield you can give the dynamic height or your row as you are giving in heightforrowatindexpath method.By doing this your cell will get the accurate height. If you have any problem in calculating dynamic height then tell me I'll post the code here for calculating it.