Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何找到在UITableView中插入更多节的正确范围?_Uitableview - Fatal编程技术网

如何找到在UITableView中插入更多节的正确范围?

如何找到在UITableView中插入更多节的正确范围?,uitableview,Uitableview,我意识到在obj-c中,计数和索引是不同的 为了让这个问题和我的问题有意义,我将使用以下示例进行演示 My_data.count从0开始 viewDidLoad之后,我获取并插入_data.count=10的部分 NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _data.count)]; [self.tableView insertSections:set with

我意识到在obj-c中,计数和索引是不同的

为了让这个问题和我的问题有意义,我将使用以下示例进行演示

My_data.count从0开始

viewDidLoad之后,我获取并插入_data.count=10的部分

NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, _data.count)];   
[self.tableView insertSections:set
              withRowAnimation:UITableViewRowAnimationFade];
这与预期的一样有效,因为我现在有了第[0-9]节。稍后我添加到_数据中,其中_data.count=12。然后,我尝试像以前一样进行简单的插入,但更改了起始索引

NSInteger sections = [self.tableView numberOfSections];
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(sections, _data.count)];   
[self.tableView insertSections:set
              withRowAnimation:UITableViewRowAnimationFade];

我可能天真地认为,这将插入[10-11]中的章节。分区=10,但我的应用程序崩溃,例外情况是“尝试插入分区12,但更新后只有12个分区”

问题是我不了解NSRange是如何工作的。这是NSRange的结构

typedef struct _NSRange {
  NSUInteger location;
  NSUInteger length;
} NSRange; 
我阅读了有关NSIndexSet的内容,并意识到它提供了一些帮助,我利用这些帮助找出了如何正确解决此问题

通过前面的例子,我了解到了索引集的范围

NSLog(@"first index %ld", (unsigned long)[set firstIndex]);
/* logs 10 */

NSLog(@"last index %ld", (unsigned long)[set lastIndex]); 
/* logs 21 ? what the heck */
有了这些信息,我知道我所要做的就是从update _data.count中减去当前的节数

NSInteger sections = [self.tableView numberOfSections];
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(sections, _data.count-sections)];   
[self.tableView insertSections:set
              withRowAnimation:UITableViewRowAnimationFade];
现在我明白了问题所在,我需要知道NSRange是如何工作的。所以我读了这篇文章,很明显我为什么会犯这个错误。第一个数字是起点,即位置,第二个数字是要移动的位置数量,即长度