【发布时间】:2014-11-17 14:04:25
【问题描述】:
我以为我在翻滚,然后又趴在脸上。
插入我的代码以将 plist 数据排序到部分后,我的 tableview 显示为空白。 请注意,我已经重新连接了 dataSource 和委托,并且知道 prepareForSegue 是不正确的——一旦我有单元格显示我将更新它(没有部分的工作)。 显然是我,但我看不到错误并且它正在构建良好。
Ps:我知道它没有针对内存管理进行优化。一旦 tableview 工作,我将声明一个 keys 属性并将它们分类,等等。 此外,没有 NSLog 语句的原因是我的 XCODE 不会在控制台中输出任何内容。
谢谢
#import "RCViewController.h"
#import "detailView.h"
@interface RCViewController ()
@end
@implementation RCViewController
@synthesize orderedPlistWords, tableView;
@synthesize alphabeticallySortedWords;
static NSString *CellIdentifier = @"Cell Identifier";
-(NSDictionary *)alphabeticallySortedWords:(NSArray *)wordsArray {
NSMutableDictionary *buffer = [[NSMutableDictionary alloc]init];
for (NSDictionary *dict in orderedPlistWords) {
NSString *word = [dict objectForKey:@"Word"];
NSString *firstLetter = [[word substringToIndex:1]uppercaseString];
if ([buffer objectForKey:firstLetter]) {
[(NSMutableArray *)[buffer objectForKey:firstLetter]addObject:dict];
}
else {
NSMutableArray *mutableArray = [[NSMutableArray alloc]initWithObjects:dict, nil];
[buffer setObject:mutableArray forKey:firstLetter];
}
}
NSArray *keys = [buffer allKeys];
for (int j; j<keys.count; j++) {
NSString *key = [keys objectAtIndex:j];
[(NSMutableArray *)[buffer objectForKey:key]sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
NSDictionary *result = [NSDictionary dictionaryWithDictionary:buffer];
return result;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSArray *keys = [self.alphabeticallySortedWords allKeys];
return [keys count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSArray *unsortedKeys = [self.alphabeticallySortedWords allKeys];
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:[indexPath section]];
NSArray *wordsForSection = [[self.alphabeticallySortedWords objectForKey:key]objectForKey:@"Word"];
NSString *word = [wordsForSection objectAtIndex:[indexPath row]];
[cell.textLabel setText:word];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *unsortedKeys = [self.alphabeticallySortedWords allKeys];
NSArray *sortedKeys = [unsortedKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [sortedKeys objectAtIndex:section];
NSArray *wordsForSection = [[self.alphabeticallySortedWords objectForKey:key]objectForKey:@"Word"];
return [wordsForSection count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self performSegueWithIdentifier:@"showDetail" sender:cell];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSArray *keys = [[self.alphabeticallySortedWords allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *key = [keys objectAtIndex:section];
return key;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
detailView *destViewController = segue.destinationViewController;
destViewController.word = [[self.orderedPlistWords objectAtIndex:indexPath.row]valueForKey:@"Word"];
destViewController.definition = [[self.orderedPlistWords objectAtIndex:indexPath.row]valueForKey:@"Definition"];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"words" ofType:@"plist"];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Word" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSMutableArray *plistWords = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
self.orderedPlistWords = [plistWords sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifier];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Word</key>
<string>Pitch</string>
<key>Definition</key>
<string>Ground players play on</string>
</dict>
<dict>
<key>Word</key>
<string>Goal</string>
<key>Definition</key>
<string>Point awarded when ball crosses goal line</string>
</dict>
<dict>
<key>Word</key>
<string>Yellow Card</string>
<key>Definition</key>
<string>Penalise player for foul</string>
</dict>
</array>
</plist>
【问题讨论】:
标签: ios objective-c uitableview plist