Reputation: 5767
I am loading a UIView with tableview. Doing a webrequest which deliveres me the data and want then to reload the data into the tableview, using the delegation-pattern. Now I am struggeling with: how do i get the data to the tableview? What is the smartes way?
I passed a NSMutableDictonary _fbContent
to the delegation method fbIDInformation:to:
which shall hold the data, but the dictonary is empty (null).
Updated: with working code. The solution was a mixture of all three answers
2013-08-02 11:02:26.403 fbTest[29347:c07] i am in FbApi mode. DOING A NEW REQUEST
-didLoadRequest 1
2013-08-02 11:02:31.181 fbTest[29347:c07] This is fbContent form fbApi (null)
2013-08-02 11:02:31.318 fbTest[29347:c07] result in fbApi: {
about = "Amazing Restaurant";
"can_post" = 1;
category = "Restaurant/cafe";
}
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "FbApi.h"
@interface LocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, FbApiDelegate>
@property (strong, nonatomic) NSString *fbID;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableDictionary *fbContent;
@property (strong, nonatomic) FbApi *fbApi;
@end
#import "LocationViewController.h"
@interface LocationViewController ()
@end
@implementation LocationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self fbLocation];
[self initTableView];
}
-(void)fbLocation {
self.fbApi = [[FbApi alloc] init];
self.fbApi.delegate = self;
// replaced:
// [self.fbApi fbIDInformation:self.fbID to:self.fbContent]; //_fbContent is passed, but stays empty...
// with:
[self.fbApi fbIDInformation:self.fbID];
}
// replaced:
// -(void)didLoadRequest {
// with:
-(void)didLoadRequest:(NSMutableDictionary *)data {
self.fbContent = data;
[self.tableView reloadData];
NSLog(@"This is fbContent form fbApi %@", self.fbContent); //_fbContent = (null)
}
// ****
// * for the sake of completeness, but irrelevant for the question
// ****
#pragma mark - table view things
-(void)initTableView {
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[NSString alloc] initWithFormat:@"About: %@", self.fbContent[@"about"]];
return cell;
}
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
@class FbApi;
@protocol FbApiDelegate <NSObject>
// replaced:
// -(void)didLoadRequest;
// with:
-(void)didLoadRequest:(NSMutableDictionary *)data;
@end
@interface FbApi : NSObject
@property (assign, nonatomic) id <FbApiDelegate> delegate;
@property (strong, nonatomic) NSMutableDictionary *json;
// replace:
// -(void)fbIDInformation:(NSString *)fbID to:(NSMutableDictionary *)dict;
// with:
-(void)fbIDInformation:(NSString *)fbID;
@end
#import "FbApi.h"
@implementation FbApi
-(id)init {
self = [super init];
if (self) {
}
return self;
}
// replaced:
// -(void)fbIDInformation:(NSString *)fbID to:(NSMutableDictionary *)dict {
// with:
-(void)fbIDInformation:(NSString *)fbID {
NSLog(@"i am in FbApi mode. DOING A NEW REQUEST");
NSString *fbquery = [NSString stringWithFormat:@"/%@", fbID];
[FBRequestConnection
startWithGraphPath:fbquery
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
// replaced:
// self.json = result;
// with:
[self.delegate didLoadRequest:result];
NSLog(@"result in fbApi: %@", result); // result has content (!)
} else {
NSLog(@"result %@, error %@", result, error);
}
}];
// removed:
//dict = self.json; // dict is here _fbContent and should be filled with result
//[self.delegate performSelector:@selector(didLoadRequest)];
}
@end
Upvotes: 1
Views: 146
Reputation: 2148
Try like this:
replace
-(void)fbLocation
to
-(void)fbLocation {
self.fbApi = [[FbApi alloc] init];
self.fbApi.delegate = self;
[self.fbApi fbIDInformation:self.fbID]; //_fbContent is passed, but stays empty...
}
replace
-(void)fbIDInformation:(NSString *)fbID to:(NSMutableDictionary *)dict
to
-(void)fbIDInformation:(NSString *)fbID
{
NSLog(@"i am in FbApi mode. DOING A NEW REQUEST");
NSString *fbquery = [NSString stringWithFormat:@"/%@", fbID];
[FBRequestConnection startWithGraphPath: fbquery
completionHandler: ^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (!error)
{
self.json = result;
NSLog(@"result in fbApi: %@", result); // result has content (!)
[self.delegate performSelector: @selector(didLoadRequest:)
withObject: result];
}
else
{
NSLog(@"result %@, error %@", result, error);
}
}];
}
and replace
- (void) didLoadRequest
to
- (void) didLoadRequest: (NSMutableDictionary*) data
{
self.fbContent = data;
[self.tableView reloadData];
NSLog(@"This is fbContent form fbApi %@", self.fbContent); //_fbContent = (null)
}
In LocationViewController.h:
@interface LocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, FbApiDelegate>
...
- (void) didLoadRequest: (NSMutableDictionary*) data;
@end
FbApi.h:
@interface FbApi : NSObject
...
-(void)fbIDInformation:(NSString *)fbID;
@end
Upvotes: 2
Reputation: 8402
Try this , hope helps u .. :)
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
@class FbApi;
@protocol FbApiDelegate <NSObject>
-(void)didLoadRequestForTableview:(NSMutableDictionary *)aDict;//change the delegate method
@end
@interface FbApi : NSObject
@property (assign, nonatomic) id <FbApiDelegate> delegate;
@property (strong, nonatomic) NSMutableDictionary *json;
-(void)fbIDInformation:(NSString *)fbID to:(NSMutableDictionary *)dict;
@end
// FbApi.m
#import "FbApi.h"
@implementation FbApi
-(id)init {
self = [super init];
if (self) {
}
return self;
}
-(void)fbIDInformation:(NSString *)fbID to:(NSMutableDictionary *)dict {
NSLog(@"i am in FbApi mode. DOING A NEW REQUEST");
NSString *fbquery = [NSString stringWithFormat:@"/%@", fbID];
[FBRequestConnection
startWithGraphPath:fbquery
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
self.json = result;
[self.delegate didLoadRequestForTableview:self.json]; // try putting hear
NSLog(@"result in fbApi: %@", result); // result has content (!)
} else {
NSLog(@"result %@, error %@", result, error);
}
}];
dict = self.json; // dict is here _fbContent and should be filled with result
//[self.delegate didLoadRequestForTableview:dict]; // change this (commment this)
}
//in your locationViewController.m
@interface LocationViewController ()
@end
@implementation LocationViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self fbLocation];
[self initTableView];
}
-(void)fbLocation {
self.fbApi = [[FbApi alloc] init];
self.fbApi.delegate = self;
[self.fbApi fbIDInformation:self.fbID to:self.fbContent];
}
-(void)didLoadRequestForTableview:(NSMutableDictionary *)aDict //implementation of delegate method
{
[self.tableView reloadData];
//use your aDict Values hear
}
// ****
// * for the sake of completeness, but irrelevant for the question
// ****
#pragma mark - table view things
-(void)initTableView {
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[NSString alloc] initWithFormat:@"About: %@", self.fbContent[@"about"]];
return cell;
}
Upvotes: 0