首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

无法在表视图单元格中显示来自App Delegate的数组数据

问题:无法在表视图单元格中显示来自App Delegate的数组数据

回答:

在iOS开发中,如果想要在表视图的单元格中显示来自App Delegate的数组数据,可以按照以下步骤进行操作:

  1. 确保你已经在App Delegate中定义了一个数组属性,并且在合适的地方初始化和填充了数据。例如,在App Delegate的.h文件中添加以下代码:
代码语言:objective-c
复制
@property (nonatomic, strong) NSArray *dataArray;

在App Delegate的.m文件中,可以在application:didFinishLaunchingWithOptions:方法中初始化和填充数据,例如:

代码语言:objective-c
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 其他初始化代码...
    
    self.dataArray = @[@"数据1", @"数据2", @"数据3"];
    
    // 其他代码...
    
    return YES;
}
  1. 在你的表视图控制器中,导入App Delegate的头文件,并使用[[UIApplication sharedApplication] delegate]来访问App Delegate的实例。例如,在表视图控制器的.m文件中添加以下代码:
代码语言:objective-c
复制
#import "AppDelegate.h"

// 其他代码...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier" forIndexPath:indexPath];
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *data = appDelegate.dataArray[indexPath.row];
    
    cell.textLabel.text = data;
    
    return cell;
}

// 其他代码...

在上述代码中,我们通过[[UIApplication sharedApplication] delegate]获取到App Delegate的实例,然后可以使用appDelegate.dataArray来访问数组数据,并将数据显示在表视图的单元格中。

需要注意的是,为了正确显示数据,你还需要在表视图的数据源方法中返回正确的行数,例如:

代码语言:objective-c
复制
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    return appDelegate.dataArray.count;
}

这样,表视图就能够正确地显示来自App Delegate的数组数据了。

推荐的腾讯云相关产品:腾讯云移动开发平台(https://cloud.tencent.com/product/mmp

希望以上回答能够帮助到你,如果有任何问题,请随时提问。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

11分33秒

061.go数组的使用场景

4分29秒

MySQL命令行监控工具 - mysqlstat 介绍

领券