开发者

Table view loads from NSDictionary, but not alphabetically

开发者 https://www.devze.com 2023-02-01 23:47 出处:网络
I am quite new to programming, but now experimenting with a table view app. I used some snippets for it, but it works pretty well. What I can\'t seem to figure out is how I can load my table view alph

I am quite new to programming, but now experimenting with a table view app. I used some snippets for it, but it works pretty well. What I can't seem to figure out is how I can load my table view alphabetically, because that looks a lot nicer. The navigation has four levels, from which the first two are table views. All the data is stored in a plist file with dictionaries. I Included the code. I have the feeling I am missing something very obvious, but I can't seem to find out what it is! Who can help me? Tha开发者_StackOverflow社区nks in advance for your time!

- (void)viewDidLoad {
// Create the table view data source array from the dictionary property
tableViewData = [[dataForCurrentLevel allKeys] retain];


Please read answer from @VdesmedT carefully. Just sort your array before passing it to the tableview like:

- (void)viewDidLoad {
// Create the table view data source array from the dictionary property
tableViewData = [[[dataForCurrentLevel allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] retain];


Use sortDescriptor to sort the array


NSDictionaries are hash tables so the keys are in fact sorted using their hashes.

You need to sort the resulting array manually using sortedArrayUsingSelector: for example but there are a lot of other method in the NSArray class.

0

精彩评论

暂无评论...
验证码 换一张
取 消