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

时间:2014-07-24 12:49:58

标签: ios uitableview nsarray

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

NSArray *listItems = @[];

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

NSArray *listItems = @[].retain;

P.S。:别忘了发布它。