gpt4 book ai didi

iOS可变数组始终为空

转载 作者:行者123 更新时间:2023-11-29 02:51:23 25 4
gpt4 key购买 nike

这是我的难题

我正在尝试在我的 View Controller 上方放置一个 TableView

View 加载并添加,我用于 TableView 的数组始终为空,但是表始终为空!

这是我的 View 头文件,其中包含我的 tableview 和可变数组属性

@interface IngredientsView : UIView <UITableViewDataSource,UITableViewDelegate>



- (id)initWithFrame:(CGRect)frame andIngredients:(NSMutableArray*)ingredientsArray;

- (id)initWithIngredients:(NSMutableArray*)ingredientsArray;

@property (weak, nonatomic) IBOutlet UITableView *notesTableView;
@property NSMutableArray* ingredients;

我的自定义 init 方法接受数组并设置它 - 这是我的自定义 init

- (id)initWithIngredients:(NSMutableArray*)ingredientsArray;
{


self.ingredients = [[NSMutableArray alloc]init];

self.ingredients=ingredientsArray;

NSLog(@"hello from init %lu", (unsigned long)[self.ingredients count]);




if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@", [self class]] owner:self options:nil] objectAtIndex:0];


UIImageView* view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ingredients background.png"]];

self.notesTableView.backgroundView=view;


self.backgroundColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:0.6f];

self.notesTableView.dataSource = self;
self.notesTableView.delegate = self;



UIGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)];

[self addGestureRecognizer:tapGesture];


[self.notesTableView reloadData];

}
return self;

在 init 方法中,self.ingredients 数组将向 NSlog 返回一个计数(例如 6 个对象)

但在我的委托(delegate) TableView 方法 numberOfSectionsFor:index 路径中它总是返回 0,就好像数组是 nil

我试过了

  • 改用实例数组
  • 合成数组并从我的 View Controller 中设置它
  • 将数组粘贴到单例上并从单例中抓取数组

所有部分返回 0?

tableview 是 nil 吗?

我通常使用 TableView Controller 并且没有太多使用 TableView 的经验是我错过了什么吗?

最佳答案

你的设置 self.ingredients=ingredientsArray;然后在下一行中将 self 设置为您加载的 Nib ,覆盖 self.在将 self 设置为 Nib 后,将 self.ingredients 分配移动到。

关于iOS可变数组始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24478439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com