本文介绍了使用NSTableView insertRowsAtIndexes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用
NSTableView :: insertRowsAtIndexes:withAnimation

I'm trying to add rows to my NSTableView dynamically usingNSTableView::insertRowsAtIndexes:withAnimation

为此,我更新了NSArray保存我的数据源,然后

To do so, I update the contents of the NSArray that holds my data source, and then

[myTableView beginUpdates];
NSRange theRange = NSMakeRange(0, [self.myTableContents count]-1);
NSIndexSet* theIndexSet = [NSIndexSet indexSetWithIndexesInRange:theRange];
[myTableView insertRowsAtIndexes:theIndexSet withAnimation:NSTableViewAnimationEffectFade];
[myTableView endUpdates];

我的问题是在重新绘制表格时出现边界错误,并计算每行的高度。我认为,该问题在文档中提到:表视图中的numberOfRows通过索引数自动增加。

My problem is that I get a bounds error as the table redraws, and calculates the height of each row. The problem, I think, is mentioned in the documentation: "The numberOfRows in the table view is automatically increased by the count of indexes."

如果myTableContents中有10个项目,并且我为所有这10个项目调用insertRowsAtIndexes,则对高度0-19的行(在myTableContents中的10行,加上因调用insertRowsAtIndexes而添加的另外10个)。

If I have 10 items in myTableContents, and I call insertRowsAtIndexes for all 10 of those items, then heightOfRow is called for rows 0-19 (the 10 rows in myTableContents, plus another 10 added as a result of calling insertRowsAtIndexes).

那么其他表如何处理呢?

So, how do other tables handle this?

推荐答案

theRange应该仅由要插入的新行的范围组成。即对于 insertRowsAtIndexes:,您应该传递并非要插入所有行的新行的索引。

theRange should only consists of the range of the new rows that are to be inserted. i.e for insertRowsAtIndexes: you should pass the indexes of the new rows that you are inserting not all the rows.

您的数据源也应该在那时更新,并且 numberOfRows 应该反映出这一点。

Your datasource should also be updated by that time and numberOfRows should reflect that.

这篇关于使用NSTableView insertRowsAtIndexes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 06:07