2

我知道很多人问这个,但所有的答案都是特定的应用程序,所以我不明白如何为我的应用程序工作。

        tableData = [NSArray arrayWithObjects:@"Chocolate Brownie", @"Mushroom Risotto", nil];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
    {
        return [tableData 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 = [tableData objectAtIndex:indexPath.row];
        return cell;` 
    }
4

3 回答 3

7

未保留ut 变量的原因,tableData并且您已通过工厂方法分配它,并且已自动释放。

在 .h 文件中,使其保留属性并将此变量与 self 一起使用。在你的代码中。

@property(nonatomic,retain) NSArray *tableData;

米,

@synthesize tableData;

然后像这样使用它:

self.tableData = [NSArray arrayWithObjects:@"Chocolate Brownie", @"Mushroom Risotto",      nil];

现在您不会收到任何错误,因为 tableData 现在已保留。

dealloc如果您不使用 ARC ,请不要忘记将其释放。

于 2012-06-24T08:22:59.220 回答
4

你必须将你的 NSArray 声明为一个属性并合成它。在你的类定义中:

@property (retain, nonatomic) NSArray *tableData;

在实施中:

@synthetize tableData = _tableData;
于 2012-06-24T08:19:39.220 回答
3

好吧,这是一个简单的修复。您在引用“tableData”的每一行都收到错误,因为它未声明。换句话说,您的应用程序从未被告知“tableData”是什么。

您可以在 .h 文件中声明“tableData”,它应该看起来像这样......

@interface yourClassName : UITableViewController 
{
    NSArray *tableData;
}

编辑:如果您只在此函数中调用此数组,请使用@grasGendarme 的答案,如果您希望在控制器中使用它,请使用此答案。

编辑2:关于您更新的问题,请在给您错误的行上检查这一点。

在此处输入图像描述

此蓝色箭头表示您已在此行的代码中设置断点。您可以右键单击错误并选择删除断点。

于 2012-06-24T08:20:01.910 回答