我正在加载带有 tableview 的 UIView。做一个 webrequest,它向我传递数据,然后希望使用委托模式将数据重新加载到 tableview 中。现在我正在苦苦挣扎:如何将数据获取到 tableview?聪明的方法是什么?
我将 NSMutableDictonary 传递给应保存数据_fbContent
的委托方法fbIDInformation:to:
,但字典为空(null)。
更新:使用工作代码。解决方案是所有三个答案的混合
调试器输出
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";
}
locationViewController.h(更新)
#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
locationViewController.m(使用工作代码更新)
#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;
}
FbApi.h(更新)
#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
FbApi.m(更新)
#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