gpt4 book ai didi

ios - 为什么我不能在选项卡式应用程序中使用初始化的数组?

转载 作者:行者123 更新时间:2023-11-29 04:51:33 25 4
gpt4 key购买 nike

在 Xcode 4.2 中开发的选项卡式应用程序中,我发现了一个令人困惑的问题:在其中一个选项卡中,有一个 TableView 来显示索引之类的内容。所以我在 viewDidLoad() 方法中初始化了一个数组。例如:

- (void)viewDidLoad
{
NSArray *array = [NSArray arrayWithObjects:@"abc", @"def", @"ghi", @"jkl", @"mno", nil];
self.arrayList = array;
[array release];

[super viewDidLoad];
}

然后我在其他方法中使用这个 arrayList:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
return cell;
}

但是每次运行 Xcode 时都会给出“EXC_BAD_ACCESS”信号。我打了一些断点,发现在viewDidLoad()中数组创建成功,但是在运行方法cellForRowAtIndexPath:(NSIndexPath *)indexPath之前,它变成了一个释放的对象。这就是我收到该信号并且应用程序崩溃的原因。那么,如何解决这个问题呢?

顺便说一句,出现问题的 View Controller 是从 UIViewController 创建的,而不是 UITableViewController。但我放置了一个 TableView 并将其数据源和委托(delegate)链接到文件的所有者。这有关系吗?

最佳答案

您不得释放该数组。 +arrayWithObjects: 便捷方法返回一个无主数组。您从未获得该阵列的所有权,因此您不得放弃所有权。删除行[array release],您将不会再看到此错误(至少不会因为这个原因)。

关于ios - 为什么我不能在选项卡式应用程序中使用初始化的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738827/

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