Malloc
Malloc

Reputation: 16276

Why is my tableview property null

I have set the delegate and datasource to the File's Owner, the outlet is set properly in the xib file. Now for the .h file:

@interface ProductsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{

    IBOutlet UITableView *objTableView;
}


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

In the .m file:

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

For the first time, self.objTableView is set properly: NSLog(@"%@",self.objTableView); gives:

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

But for next time I got a (null) table view object and so the reloadData doesn't refresh the table view. How to fix that, thanx in advance.

EDIT:

I am using the Afnetworking method JSONRequestOperationWithRequest like the following:

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.."];

Actually, JSONRequestOperationWithRequestrun asynchronous and so not in the main thread, however it turned that the UI updates should be done in the main thread, so I need to remove [self.objTableView reloadData]; outside that method. But where? how to make sure to run it in the main thread after JSONRequestOperationWithRequest will finish?

Upvotes: 0

Views: 887

Answers (3)

Matt
Matt

Reputation: 2411

I suggest you create the following method:

- (void)setObjTableView:(UITableView *)tableView {
    _objTableView = tableView;
}

Then set a breakpoint on the _objTableView = tableView line, which should let you know what causes _objTableView to become nil.

Upvotes: 0

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

Have you tried setting a Watchpoint on objTableView?

In your -viewDidLoad set a breakpoint. When the debugger stops, secondary-click on objTableView in the variable list. Click on "Watch 'objTableView'". It will break anytime the value of objTableView changes.

It should let you know exactly when the value changes.

Upvotes: 2

Rob
Rob

Reputation: 437442

Are you sure you're looking at self.objTableView (the property's accessor method) and not objTableView (the instance variable you manually defined)? Do you have a @synthesize line? If you omitted your @synthesize line, it would have effectively done @synthesize objTableView = _objTableView; for you, defining an instance variable called _objTableView for your property called objTableView, and thus your manually defined instance variable, objTableView would never have been initialized.

It is recommended that you remove the manually defined instance variable and let the compiler synthesize that for you, and just define the property, thus:

@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

The compiler will generate the instance variable for you, and unless to manually write your own @synthesize line, the compiler will name the instance variable _objTableView. If you ever need to reference the instance variable for your objTableView property (generally only needed in initializer and dealloc methods), remember to include the leading underscore. (The convention of the underscore is to minimize chances that you accidentally refer to the instance variable when you actually intended to use the self.objTableView accessor getter method.

Upvotes: 2

Related Questions