2013-08-16 121 views
2

我想對從MPMediaQuery生成的UITableView進行基本排序。Sorting UITableView

我想要的只是一個基本的歌曲列表,將歌曲分爲A,B,C,D等部分。不應該很難,但是我試過的每個教程都會結束完全給我這些結果。

任何幫助將是美好的。這是我的基本代碼。我知道這是不是甚至接近有我想要的成就,但我想它會更容易爲經驗豐富的開發人員,以幫助我從這個角度再惹我在

如何生成我的數據:

MPMediaQuery *musicLibraryQuery = [[MPMediaQuery alloc] init]; 
NSArray *musicLibraryArray = [musicLibraryQuery items]; 
songLibrary = [NSMutableArray arrayWithArray:musicLibraryArray]; 

這些都是我的UITableView方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [songLibrary count]; 
} 

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"SongCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    MPMediaItem *song = [[songLibrary objectAtIndex:[indexPath row]] representativeItem]; 

    cell.textLabel.text = [song valueForProperty:MPMediaItemPropertyTitle]; 

    return cell; 
} 
+0

請試試這個 - > http://stackoverflow.com/questions/5820979/objective-c-sort-an-array-of-串 – Vinodh

回答

0

ü需要例如排序陣列


songLibrary = [musicLibraryArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; 


0

如果有字典在你的Array對象,要排序的perticular鍵數組,那麼這樣做:

靜態的NSString * const的keyToSortBy = @ 「keyToSortBy」;

NSArray * sortedArray = [array sortedArrayUsingComparator:^(id obj1, id obj2) { 
    NSString *s1 = [obj1 objectForKey:keyToSortBy]; 
    NSString *s2 = [obj2 objectForKey:keyToSortBy]; 
    return [s1 caseInsensitiveCompare:s2]; 
}]; 
0

利用這一點,也許這將是有益的

songLibrary=[songLibrary sortedArrayUsingSelector:@selector(compare:)];