objective-c - NSTableView 中的 NSTextField 动态高度

标签 objective-c cocoa nstableview

NSTableView 中有一个 NSTextField。

调整窗口大小时文本的高度增加。 但不能在表格 View 中增长单元格。

在这种情况下,table view cell的高度你喜欢怎么改变?

简而言之,当调整 TextView 的大小时,我想更改单元格的大小。

像这样:(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;
}

最佳答案

使用以下方法根据 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];
}

关于objective-c - NSTableView 中的 NSTextField 动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23055483/

相关文章:

cocoa - 如何将崩溃报告的电子邮件地址重定向到我的电子邮件(对于我的 cocoa 应用程序)

macos - 当 NSWindow 调整大小时调整表列的大小

objective-c - NSTableView + 删除键

ios - 如何禁用 UINavigationController Root View Controller 的自动旋转?

ios - 当 Controller 在 Storyboard 中时,如何按类名初始化 Controller ?

ios - 在执行方法而不是新 View Controller 的 UITabBar 上添加选项卡

iphone - NSString 比较抛出异常 SIGABT

objective-c - 核心数据: Observing all changes on Entity of certain type

objective-c - 添加到登录项 - 沙盒友好的解决方案

objective-c - 单击 NSTextField 下方