【问题标题】:UITableView blankUITableView 空白
【发布时间】: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


    【解决方案1】:

    你有一个属性,alphabeticallySortedWords,你的 UITableViewDataSource 方法引用了它,但你从不填充。您有一个类似名称的方法,alphabeticallySortedWords,但它需要一个数组参数。

    每当您遇到此类问题时。在numberOfRowsInSectionnumberOfSectionsInTableView 中放置断点。这将确认这些方法完全被调用(即确保您的场景的基类设置正确,确保您已设置表格视图属性的数据源等)。在这种情况下,如果您这样做了,您会立即发现 alphabeticallySortedWordsnil


    顺便说一句,这里也有其他错误:

    numberOfRowsInSection 中,有一行内容是:

    NSArray *wordsForSection = [[self.alphabeticallySortedWords objectForKey:key] objectForKey:@"Word"];
    

    应该是:

    NSArray *wordsForSection = [self.alphabeticallySortedWords objectForKey:key];
    

    同样,在cellForRowAtIndexPath 中,您有几行说:

    NSArray *wordsForSection = [[self.alphabeticallySortedWords objectForKey:key] objectForKey:@"Word"];
    NSString *word = [wordsForSection objectAtIndex:[indexPath row]];
    

    他们应该是:

    NSArray *wordsForSection = [self.alphabeticallySortedWords objectForKey:key];
    NSString *word = [[wordsForSection objectAtIndex:[indexPath row]] objectForKey:@"Word"];
    

    在这两种情况下,您都在错误的时间提取了Word

    【讨论】:

    • 感谢您抽出宝贵时间回答。我删除了参数并且它已经运行了。对不起我的无知。
    • 好吧,你已经编写了一个构建这个结构的方法,alphabeticallySortedWords:,所以 (a) 在你读完 plist 之后调用这个例程; (b) 更改该例程以将结果实际保存到您类似命名的属性中。坦率地说,我还会更改该方法的名称,以消除属性和构建该属性的方法之间的任何混淆。
    • 通过从方法中删除参数,应用程序按计划运行。但是,这样做是错误的 - 我应该在 viewdidload 中调用该方法吗?如果我在胡扯,我很抱歉 - 我真的很感谢你的帮助☺
    • 我同意,如果您删除代码神奇地工作的参数。问题是你多次调用这个alphabeticallySortedWords,你可能不想每次都重建这个结构。当您定义了 3 个单词时很好,但这种方法不可扩展。这就是为什么我建议您 (a) 保留该方法,(b) 重命名它以避免与属性混淆,(c) 更改它以将结果保存在 alphabeticallySortedWords 属性中; (d) 确保在加载 plist 后调用此方法,以便正确设置属性。
    • 非常感谢您的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多