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

标签 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

最佳答案

NSArray *listItems = @[];

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

NSArray *listItems = @[].retain;

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

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

相关文章:

ios - 如何设置这种类型的底部边框文本框?

ios - 从 iOS 10.3 之前的 SecCertificate 获取 SecKey

ios - 在 UISearchDisplayController 中添加 UISegmentedControl

ios - Xcode Swift 回到上一个viewController(TableViewController)

ios - 如何查看对象是否包含在嵌入式 NSArray 中,然后获取集合中的其他项目

ios - 无法使用本地化设置语言

ios - iOS 中的泰米尔语字体

ios - 核心数据跳过获取函数 Swift

search - 在NSArray中搜索字符串

ios - 如何计算 Swift 数组中元素的出现次数?