gpt4 book ai didi

ios - 应用程序在行的 UItableView 单元格中崩溃

转载 作者:行者123 更新时间:2023-11-29 02:47:28 24 4
gpt4 key购买 nike

我正在尝试使用 tableView 创建一个示例应用程序并在该 tableview 上填充 NSArray 。当我使用

声明数组时每次滚动时它都会崩溃
NSArray *listItems = @[];

当我将数组声明改回

NSArray * listItems = [[NSArray alloc]initWithObjects:@"a",

@"b",
@"c",
@"d",
@"e",
@"f",
@"g",
@"h",
@"i",
@"j",
@"k",
@"l",
@"m",
@"n",
@"o",
@"p",
@"q",
@"r",
@"s",
@"t",
@"u",
@"v",
@"w",
@"x",
@"y",
@"z",
@"1",
@"2",
@"3 ",
@"4 ",
@"5 ",
@"6 ",
@"7 ",
@"8 ",
@"9",
@"0",
@"12",nil]retain];

效果很好! 。两者有什么区别?我正在使用非 ARC 环境。这是我的代码

#import "MyPopOverView.h"



@implementation MyPopOverView

@synthesize tableListiTems;

@synthesize lists;

@synthesize listDict;






/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

tableListiTems.delegate = self;
tableListiTems.dataSource = self;

listDict = @{
@"a":
@"b", // Register,
@"c":
@"d",
@"e":
@"f",
@"g":
@"h",
@"i":
@"j",
@"k":
@"l ",
@"m":
@"n"};



lists = @[
@"a",
@"b",
@"c",
@"d",
@"e",
@"f",
@"g",
@"h",
@"i",
@"j",
@"k",
@"l",
@"m",
@"n",
@"o",
@"p",
@"q",
@"r",
@"s",
@"t",
@"u",
@"v",
@"w",
@"x",
@"y",
@"z",
@"1",
@"2",
@"3 ",
@"4 ",
@"5 ",
@"6 ",
@"7 ",
@"8 ",
@"9",
@"0",
@"12",
];


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [lists count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

cell.textLabel.text = [lists objectAtIndex:indexPath.row];

return cell;
}




- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end

最佳答案

NSArray *listItems = @[];

返回自动释放的对象:[NSArray array]。请添加保留:

NSArray *listItems = @[].retain;

P.S.: 不要忘记释放它。

关于ios - 应用程序在行的 UItableView 单元格中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24934166/

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