gpt4 book ai didi

ios - 为什么我的 tableview 属性为 null

转载 作者:行者123 更新时间:2023-11-28 19:10:56 25 4
gpt4 key购买 nike

我已将 delegatedatasource 设置为 File's Owner, socket 已在 xib 中正确设置> 文件。现在对于 .h 文件:

@interface ProductsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{

IBOutlet UITableView *objTableView;
}


@property(nonatomic,strong)IBOutlet UITableView *objTableView;

.m文件中:

NSLog(@"%@",self.objTableView);
[self.objTableView reloadData];

第一次正确设置self.objTableView:NSLog(@"%@",self.objTableView);给出:

<UITableView: 0x1d9a5800; frame = (4 54; 532 660); clipsToBounds = YES; autoresize = W+H; 

但是下次我得到一个(null) TableView 对象,所以reloadData 不会刷新 TableView 。如何解决这个问题,提前谢谢。

编辑:

我正在使用 Afnetworking 方法 JSONRequestOperationWithRequest,如下所示:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){

[SVProgressHUD dismiss];
//Get the response from the server
//And then refresh the tableview

[self.objTableView reloadData];//I shouldn't put this here, it should be in the main thread
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response,NSError *error, id JSON){

[SVProgressHUD dismiss];

//Alert the error message

}];
[operation start];
[SVProgressHUD showWithStatus:@"Searching for products, please wait.."];

实际上,JSONRequestOperationWithRequest是异步运行的,所以不在主线程中运行,但是 UI 更新应该在主线程中完成,所以我需要删除[self.objTableView reloadData]; 在该方法之外。但是哪里?如何确保在 JSONRequestOperationWithRequest 完成后在主线程中运行它?

最佳答案

您确定您正在查看 self.objTableView(属性的访问器方法)而不是 objTableView(您手动定义的实例变量)吗?你有 @synthesize 行吗?如果您省略了 @synthesize 行,它会有效地为您完成 @synthesize objTableView = _objTableView;,为您定义一个名为 _objTableView 的实例变量您的属性名为 objTableView,因此您手动定义的实例变量 objTableView 永远不会被初始化。

建议去掉手动定义的实例变量,让编译器为你合成,只定义属性,这样:

@interface ProductsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

// Following lines removed. Do not define the instance variable.
// Let the compiler synthesize it for you.
//
// {
// IBOutlet UITableView *objTableView;
// }

@property(nonatomic,strong)IBOutlet UITableView *objTableView;

@end

编译器会为您生成实例变量,除非手动编写您自己的@synthesize行,否则编译器会将实例变量命名为_objTableView。如果您需要为您的 objTableView 属性引用实例变量(通常仅在初始化程序和 dealloc 方法中需要),请记住包含前导下划线。 (下划线的约定是为了最大限度地减少在您实际打算使用 self.objTableView 访问器 getter 方法时意外引用实例变量的可能性。

关于ios - 为什么我的 tableview 属性为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15594804/

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