Using NSTableView insertRowsAtIndexes

丶灬走出姿态 提交于 2019-12-13 03:05:41

问题


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

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

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."

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?


回答1:


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.

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




回答2:


Reload Table first time and after that use this..

[myTableView beginUpdates];  
NSIndexSet* theIndexSet = [NSIndexSet indexSetWithIndex:[self.myTableContents count]-1];
[myTableView insertRowsAtIndexes:theIndexSet withAnimation:NSTableViewAnimationEffectFade];
[myTableView endUpdates];


来源:https://stackoverflow.com/questions/21892829/using-nstableview-insertrowsatindexes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!