开发者

UITableView crash

开发者 https://www.devze.com 2023-03-14 17:44 出处:网络
Anyone got any ideas why this Table View code is crashing? This, in my viewDidLoad: itemArray = [NSArray arrayWithObjects:@\"1\", @\"2\", @\"3\", @\"4\", @\"5\", @\"6\", @\"7\", nil];

Anyone got any ideas why this Table View code is crashing?

This, in my viewDidLoad:

itemArray = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];

Then, this in my cellForRowAtIndex开发者_如何学运维Path method:

cell.textLabel.text = [NSString stringWithFormat:@"Item (%@)", [itemArray objectAtIndex:indexPath.row]]; 

When I scroll down (i.e. so that object 1 goes off screen) then scroll back to try and see object 1, it crashes at this previous line.

It's fine if I replace the offending line with something like this:

cell.textLabel.text = @"test";

UPDATE: Answer was that the array was not being retained. This line fixed the problem:

itemArray = [[NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil] retain];

OR

itemArray = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", nil];


Your itemArray seems not to be retained. arrayWithObjects: returns an autoreleased object. You need to retain or copy it, or use the syntactic sugar of a retaining property.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号