NSTextField在NSTableView中的动态高度

时间:2014-04-14 08:33:36

标签: objective-c cocoa nstableview

NSTableView中有一个NSTextField。

窗口大小调整中文本的高度会增加。 但不要在表格视图中增长单元格。

在这种情况下,表视图单元格的高度会改变你喜欢的方式吗?

简而言之,在调整文本视图大小时,我想更改单元格的大小。

像这样:( Mac商店)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 个答案:

答案 0 :(得分:0)

根据textview内容使用以下方法调整单元格高度:

- (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];
}

答案 1 :(得分:0)

如果您知道,有两种方法可用于绘制单元格 1-viewForTableColumn 2- objectValueForTableColumn 在上面的方法中,当你绘制文本字段时,你可以给出动态高度或你在给予heightforrowatindexpath方法的行。通过这样做,你的单元格将获得准确的高度。如果您在计算动态高度时遇到任何问题,请告诉我我将在此处发布代码以进行计算。

相关问题