我有一个UITextField和一个UICollectionView。根据文本字段中的字符,应该调用shouldChangeCharactersInRange并重新加载collectionview。用户快速输入字符时,应用程序崩溃并显示“ NSInternalInconsistencyException”。
这是我的代码
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemCell" forIndexPath:indexPath];
Item *item = super.itemArray[indexPath.row];
cell.itemName.text = item.name;
cell.detail.text = item.detail;
if (item.image == nil )
{
NSBlockOperation * op = [NSBlockOperation blockOperationWithBlock:^{
NSData * imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:item.imageURL]];
if (imgData == nil)
{
UIImage * image = [UIImage imageNamed:@"logo1"];
item.image = image;
}
else
{
UIImage * image = [UIImage imageWithData:imgData];
item.image = image;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.itemCollectionView reloadItemsAtIndexPaths:@[indexPath]];
});
}];
[_itemQueue addOperation:op];
}
else
{
cell.image.image = item.image;
}
return cell;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
searchStr = [Validation trim:searchStr];
[super.itemArray removeAllObjects];
for (Item *searchItem in _allItemsArray)
{
if(!([searchItem.name rangeOfString:searchStr options:NSCaseInsensitiveSearch].location == NSNotFound))
{
[super.itemArray addObject:searchItem];
}
}
[_itemCollectionView reloadData];
return TRUE;
}
这在用户缓慢输入字符但在用户快速输入文本时可以正常工作,它会崩溃,并出现异常“ NSInternalInconsistencyException”,原因:“试图从第0部分删除仅包含0个项目的第0部分中的项目2”。 / p>
似乎两次删除了该行。可能的解决方案是什么?