如何在基于视图的NSTableView中添加行跨度? [英] How to add row-span in view-based NSTableView?

查看:314
本文介绍了如何在基于视图的NSTableView中添加行跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获得一个表布局如下所示使用在Lion中引入的基于视图的NSTableViews。



有多种方法描述的单元格基于NSTableViews



我有一个groupTableView有一个列,并显示代表组。我也有itemTableView有列表示组的项目。我使用相同的委托/ datasource方法与if-else语句检查正在使用哪个NSTableView,并相应地响应正确的行,单元格视图等。此外,我实现以下委托方法来调整组表视图的行高度等于组中行的项目行高度的总和:

   - (CGFloat)tableView: (NSTableView *)tableView heightOfRow:(NSInteger)row 
{
if(tableView == self.groupTableView){
NSUInteger firstRowIndexForGroup = ...;
NSUInteger lastRowIndexForGroup = ...;
CGFloat groupHeight = 0.0;
for(NSUInteger currentRowIndex = firstRowIndexForGroup; currentRowIndex< = lastRowIndexForGroup; currentRowIndex ++){
groupHeight + = [self.itemTableView rectOfRow:lastRowIndexForGroup] .size.height;
}
return groupHeight - [self.itemTableView intercellSpacing] .height;
} else {
return self.itemTableView.rowHeight;
}
}

您还必须调用 noteHeightOfRowsWithIndexesChanged:每次表视图需要一个组视图,因为高度根据组中的行数而变化。

   - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 
{
if(tableView == self.groupTableView){
GroupRowView * view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
//配置视图
[tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]];
return view;
} else {
ItemRowView * view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
//配置视图
return view;
}
}

我禁用了行选择,水平和垂直滚动条组表视图。我也为水平网格固体两个表视图。最后,我将组表视图直接放置在项目表视图旁边,两者之间没有间隙,因此它看起来好像它只是单个表视图中的另一列。结果是一个完美的实现,在表视图之间有零滞后。对用户来说,它似乎是一个单一的表视图。


I'm trying to get a table layout as shown below working with view-based NSTableViews that have been introduced in Lion.

There were multiple approaches described for cell-based NSTableViews, e.g. Mimic the artwork column, but these don't really apply for view based table views.

The idea is that the table gets populated by an array of objects, in one (or more) column spanning rows indicating that objects share some data in common. numberOfRowsInTableView: returns to total number of items (19 in the case of the attached image).

Has anyone tried something like this?

Layout

解决方案

I was able to do this by using two separate NSTableViews that have their NSScrollView's scrolling synchronized. To learn how to synchronize multiple scroll view's (with the exact subclass code) read Scroll View Programming Guide for Mac - Synchronizing Scroll Views

I have groupTableView that has a single column and shows the views that represent the group. I also have itemTableView that has columns representing the items of the group. I use the same delegate/datasource methods with if-else-statements to check which NSTableView is being used and respond accordingly with the proper # of rows, cell view, etc.. Additionally, I implement the following delegate method to adjust the group table view's row height to be equal the sum of the item row heights of the rows in the group:

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    if (tableView == self.groupTableView) {
        NSUInteger firstRowIndexForGroup = ...;
        NSUInteger lastRowIndexForGroup = ...;
        CGFloat groupHeight = 0.0;
        for (NSUInteger currentRowIndex = firstRowIndexForGroup; currentRowIndex <= lastRowIndexForGroup; currentRowIndex++) {
            groupHeight += [self.itemTableView rectOfRow:lastRowIndexForGroup].size.height;
        }
        return groupHeight - [self.itemTableView intercellSpacing].height;
    } else {
        return self.itemTableView.rowHeight;
    }
}

You must also call -noteHeightOfRowsWithIndexesChanged: every time the table view needs a group view because the height changes according to the number of rows in the group.

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if (tableView == self.groupTableView) {
        GroupRowView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
        // configure view
        [tableView noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndex:row]];
        return view;
    } else {
        ItemRowView *view = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
        // configure view
        return view;
    }
}

I disabled row selection and horizontal and vertical scroll bars on the group table view. I also made the horizontal grid solid for both table views. Finally, I position the group table view directly next to the item table view with no gap in between so it appears as if it's simply another column in a single table view. The result is a flawless implementation that has zero lag between the table views. To the user, it appears as if it's a single table view.

这篇关于如何在基于视图的NSTableView中添加行跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆