ios - 尝试滚动 UITableView 时出现异常

标签 ios objective-c uitableview

我有一个非常简单的应用程序来学习如何使用 UITableView 中的部分,但有一个异常(exception) -

2013-09-17 08:46:19.956 Sections[4497:c07] * -[__NSArrayI objectAtIndex:]: message sent to deallocated instance 0x9566d40

整个方法如下 - 需要帮助。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.names = dict;
    NSArray *array = [[_names allKeys] sortedArrayUsingSelector:@selector(compare:)];
    _keys = array;
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    NSLog(@"%lu", (unsigned long)[_keys count]);
    return [_keys count];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [_keys objectAtIndex:section];
    NSArray *nameSection = [_names objectForKey:key];
    return [nameSection count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger section = [indexPath section];
    NSUInteger row = [indexPath row];

    NSString *key = [_keys objectAtIndex:section];
    NSArray *nameSection = [_names objectForKey:key];

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];

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

    cell.textLabel.text = [nameSection objectAtIndex:row];
    return cell;
}

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *key = [_keys objectAtIndex:section];
    return key;
}

最佳答案

你必须像这样保留 _keys 数组:

_keys = [[[_names allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];

关于ios - 尝试滚动 UITableView 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18841511/

相关文章:

iphone - 如何在不移除 Round Rect Button 周围边框的情况下消除 Round Rect Button 周围的间隙?

ios - UITableViewCell 中 UIImageView 上的 UITapGestureRecognizer

iphone - 如何删除sqlite中表列的自动递增?

objective-c - 使用模态部分 curl ,我想反转动画。我该怎么做?

objective-c - 使用 substringFromIndex : with NSData

ios - 在另一个 UIViewController 中显示缩放的 UIViewController

iphone - 在 UITableView 样式中自定义 selectedBackgroundView 出站分组

iphone - 如何访问 UITableViewCellAccessoryDisclosureIndicator 的图像文件?

ios - 如何遍历字典,通过快速按下按钮加载下一个项目

ios - TabBar 覆盖导航堆栈中的 View Controller 内容