【问题标题】:NSTextField dynimic height in NSTableViewNSTableView 中的 NSTextField 动态高度
【发布时间】:2014-04-14 08:33:36
【问题描述】:

在 NSTableView 中有一个 NSTextField。

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

在这种情况下,表格视图单元格的高度你喜欢怎么改变?

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

像这样:(Mac 商店)

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

【问题讨论】:

    标签: objective-c cocoa nstableview


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多