gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:26:22 25 4
gpt4 key购买 nike

我有一个非常简单的应用程序来学习如何使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com