我有NSArray,它包含我的庫中的所有ALAssets。我需要通過這些資產的名稱來過濾它。我試着創建第二個數組只與資產的名稱(通過ALAssetRepresention)。但之後我無法顯示縮略圖並保存資源鏈接。 那麼我該如何解決這個問題?ALAssets的NSPredicate和NSArray
1
A
回答
1
您需要將所有對象(如姓名,縮略圖和網址)存儲在一個對象中,然後將所有這些對象存儲在一個數組中。然後用這個代碼:
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"ItemTitle"
ascending:YES
selector:@selector(localizedStandardCompare:)];
[arrData sortUsingDescriptors:[NSArray arrayWithObject:sorter]];
我存儲在對象中包含的NSString名稱爲「ItemTitle」
1
我解決這個問題,接下來的路名。 我創建了包含2個值的對象SearchItems
:NSString *name
和ALAsset *asset
。 當我列舉的庫文件,我設置的值:
ALAssetRepresentation *rep = [result defaultRepresentation];
SearchItem *newItem = [[SearchItem alloc] init];
[newItem setName:[rep filename]];
[newItem setAsset:result];
然後我將該產品添加到我的NSMutableArray
: [assetsItems addObject:newItem];
,最後我用NSPredicate通過name
過濾我的數組:
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", searchText];
_searchResults = [self.assetsItems filteredArrayUsingPredicate:resultPredicate];
現在陣列準備取回。表視圖示例:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"SearchCell";
SearchCell *cell = (SearchCell *)[self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[SearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// Configure the cell...
SearchItem *searchItem = [_searchResults objectAtIndex:indexPath.row];
ALAsset *currentAsset = searchItem.asset;
ALAssetRepresentation *rep = [currentAsset defaultRepresentation];
[cell.nameLabel setText:[rep filename]];
[cell.thumbnail setImage:[UIImage imageWithCGImage:[currentAsset thumbnail]]];
return cell;
}
就是這樣。我希望將來它可以幫助別人。
相關問題
- 1. NSPredicate和NSArray
- 2. NSPredicate和NSArray的令牌
- 3. NSPredicate of multidimensional NSArray
- 4. 對CGPoints NSArray的NSPredicate
- 5. NSPredicate,在NSArray中搜索,內NSArray的NSDict
- 6. NSArray上的多個NSPredicate
- 7. 濾波器的NSArray與NSPredicate
- 8. NSPredicate對抗iphone中的NSArray
- 9. 過濾NSArray的使用NSPredicate
- 10. NSArray與NSPredicate使用NOT IN
- 11. 從NSArray動態生成NSPredicate
- 12. 使用NSPredicate篩選NSArray
- 13. 使用NSPredicate過濾NSArray
- 14. NSArray的篩選與NSNumber的NSArray的的用NSPredicate
- 15. 濾波器的NSArray用的NSNumber的NSArray的與NSPredicate
- 16. NSArray的NSArray中的對象的屬性的NSPredicate
- 17. 構建用於NSArray的ID的NSPredicate
- 18. 帶有NSPredicate過濾器問題的NSArray
- 19. 評估NSArray上的NSPredicate(沒有過濾)
- 20. 比較NSArray的for循環與nspredicate
- 21. 過濾的NSArray/NSDictionary中使用NSPredicate
- 22. NSPredicate與NSArray的NSNumbers(核心數據)
- 23. 使用NSPredicate過濾NSDictionary對象的NSArray
- 24. 搜索結果總是0(NSArray的和NSPredicate)
- 25. 使用NSPredicate按關鍵字過濾NSArray
- 26. NSDictionary VS NSArray + NSPredicate:更快/推薦
- 27. 如何使用NSPredicate篩選NSArray
- 28. 在NSArray上使用NSPredicate時崩潰
- 29. 重新排序ALAssets
- 30. 如何在NSStrings的NSArray上使用NSPredicate中的鍵路徑?
我不確定我是否理解你的想法。 :(我不需要排序這些項目,我需要過濾它的搜索視圖。 –