问题描述
我有一个音乐应用程序,我发展,而我管理TableViews,我为他们整理成部分。这通常是一个简单的过程,但我用我管理的管理数据的方式,它可能不是一个完全简单的任务。
I have a Music App I am developing, and while I'm managing the TableViews, I'm sorting them into Sections. This is usually an easy process, but the with the way I manage I manage the data, it might not be a completely simple task.
我知道这是不是做到这一点的最好办法,但由于细胞具有多个属性(titleLabel,为textLabel,ImageView的),我保存在3个独立的阵列中的所有数据。当标题组织的歌曲,为节头,我用的是predicate通过此code设置titleLabel,所以。
I know it's not the best way to do it, but as the cell has multiple properties (titleLabel, textLabel, imageView), I store all the data in 3 separate arrays. When organising the songs by title, for section header, I use a predicate to set the titleLabel, so by using this code.
NSArray *predict = [mainArrayAll filteredArrayUsingPredicate:predicate];
cell.titleLabel.text = [predict objectAtIndex:indexPath.row];
不幸的是,我也有一个otherArrayAll和其他。这些都如歌手名,专辑封面等数据需要相对于他们的歌曲。有没有办法以同样的方式在mainArrayAll阵列重新排列这些阵列?所以相对的数据放在一起?
Unfortunately, I also have a otherArrayAll, and others. These all have data such as the artist name, album artwork and so need to be relative to the songs they are for. Is there a way to reorder these other arrays in the same way the mainArrayAll array is? So the relative data is kept together?
推荐答案
在上述情况下,我会建议你创建模型类的实现来存储所有的值。继是这里的最佳方式。
In the above case I would suggest you to create a model class implementation to store all the values. Following MVC architecture is the best way here.
有关如: -
@interface Music : NSObject {}
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *artistName;
@property (nonatomic, strong) NSString *albumName;
//etc...
@end
现在,如果你想创建音乐的数组,这样做的:
Now if you want to create an array of music, do it as:
Music *myMusic1 = [[Music alloc] init];
myMusic1.title = @"name";
myMusic1.artistName = @"artist";//etc.. do this in a loop based on other contents array
NSMutableArray *array = [NSMutableArray arrayWithObjects:myMusic1, myMusic2, ... nil];
现在为排序此阵:
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
return [a.title compare:b.title];
}];
如果你想使用predicates:
If you want to use predicates:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains %@", @"title", @"name"];
NSArray *predict = [mainArrayAll filteredArrayUsingPredicate:predicate];
如果你想在一个表视图左右使用这些属性,你可以使用它作为:
If you want to use these properties in a table view or so, you can use it as:
NSString *title = [[sortedArray objectAtIndex:indexPath.row] title];
NSString *artistName = [[sortedArray objectAtIndex:indexPath.row] artistName];
这篇关于多NSMutableArrays,一个predicate排序,其他比较重排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!