iphone - Xcode "Use of undeclared identifier"

标签 iphone xcode uitableview

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

        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;` 
    }

最佳答案

原因是 ut tableData 变量没有被保留,并且您已经通过工厂方法分配了它,并且已经自动释放了。

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

@property(nonatomic,retain) NSArray *tableData;

以 .m 为单位,

@synthesize tableData;

然后像这样使用它:

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

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

如果您不使用 ARC,请不要忘记在 dealloc 中释放它。

关于iphone - Xcode "Use of undeclared identifier",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11175938/

相关文章:

ios - Facebook 图形 API 浏览器 : get friends of friends

iphone - 辅助功能:适用于 iPhone/iOS 的辅助应用程序

ios - 使用 URL 删除 AVAsset

iphone - Swift:如何以编程方式设置 iphone 音量

ios - 类在两者中都实现,将使用两者之一。哪个是未定义的

ios - 从 Xcode 9.4 升级到 Xcode 11 以及 Swift 3.0 升级到 Swift 4.2 后,在 iOS 应用程序中使用 Realm 时指针损坏

ios - UITableView 在 View 出现之前重置其背景颜色

iOS:如何在 Xcode 11 中自动升级我的 CFBundleVersion 和 CFBundleShortVersionString? (我的旧脚本不再起作用了)

ios - 注册 UITableViewCell 不工作

ios - 如果有条件,加载 TableView ,否则加载 web View xcode