objective-c将数据从委托传递到初始调用类(获取uitableview的数据)
我正在用tableview加载UIView。使用委托模式执行webrequest,该请求将向我提供数据,然后希望将数据重新加载到tableview中。现在我正在努力: 如何将数据获取到tableview?最聪明的方法是什么 我将NSMutableDictionaryobjective-c将数据从委托传递到初始调用类(获取uitableview的数据),objective-c,uitableview,delegation,reloaddata,Objective C,Uitableview,Delegation,Reloaddata,我正在用tableview加载UIView。使用委托模式执行webrequest,该请求将向我提供数据,然后希望将数据重新加载到tableview中。现在我正在努力: 如何将数据获取到tableview?最聪明的方法是什么 我将NSMutableDictionary\u fbContent传递给委托方法fbIDInformation:to:,该方法将保存数据,但Dictionary为空(null) 更新:使用工作代码。解决方案是三种答案的混合 调试器输出 locationViewControll
\u fbContent
传递给委托方法fbIDInformation:to:
,该方法将保存数据,但Dictionary为空(null)
更新:使用工作代码。解决方案是三种答案的混合
调试器输出
locationViewController.h(已更新)
#导入
#导入“AppDelegate.h”
#导入“FbApi.h”
@接口位置ViewController:UIViewController
@属性(强,非原子)NSString*fbID;
@属性(弱、非原子)IBUITableView*tableView;
@属性(强,非原子)NSMutableDictionary*fbContent;
@性质(强,非原子)FbApi*FbApi;
@结束
locationViewController.m(使用工作代码更新)
#导入“LocationViewController.h”
@接口位置ViewController()
@结束
@实现位置视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
[自我定位];
[自初始化表视图];
}
-(无效)地点{
self.fbApi=[[fbApi alloc]init];
self.fbApi.delegate=self;
//取代:
//[self.fbApi fbIDInformation:self.fbID to:self.fbContent];//\u fbContent已传递,但保持为空。。。
//与:
[self.fbApi fbIDInformation:self.fbID];
}
//取代:
//-(无效)请求{
//与:
-(void)didLoadRequest:(NSMutableDictionary*)数据{
self.fbContent=数据;
[self.tableView重载数据];
NSLog(@“这是fbContent表单fbApi%@”,self.fbContent);/\u fbContent=(null)
}
// ****
//*为完整起见,但与问题无关
// ****
#pragma标记-表视图内容
-(void)initTableView{
self.tableView.dataSource=self;
self.tableView.delegate=self;
}
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回10;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];
}
cell.textLabel.text=[[NSString alloc]initWithFormat:@“About:%@”,self.fbContent[@“About”];
返回单元;
}
FbApi.h(更新版)
#导入
#导入“AppDelegate.h”
@FbApi类;
@协议代理
//取代:
//-(无效)请求;
//与:
-(void)didLoadRequest:(NSMutableDictionary*)数据;
@结束
@接口FbApi:NSObject
@属性(赋值,非原子)id委托;
@属性(强,非原子)NSMutableDictionary*json;
//替换:
//-(void)fbIDInformation:(NSString*)fbID to:(NSMutableDictionary*)dict;
//与:
-(void)fbIDInformation:(NSString*)fbID;
@结束
FbApi.m(更新)
#导入“FbApi.h”
@FbApi的实现
-(id)init{
self=[super init];
如果(自我){
}
回归自我;
}
//取代:
//-(void)fbIDInformation:(NSString*)fbID to:(NSMutableDictionary*)dict{
//与:
-(无效)fbID信息:(NSString*)fbID{
NSLog(@“我处于FbApi模式,正在执行新请求”);
NSString*fbquery=[NSString stringWithFormat:@”/%@,fbID];
[FBI]请求连接
startWithGraphPath:fbquery
completionHandler:^(FBRequestConnection*连接,
身份证结果,
N错误*错误){
如果(!错误){
//取代:
//self.json=结果;
//与:
[self.delegate请求:结果];
NSLog(@“fbApi中的结果:%@”,结果);//结果包含内容(!)
}否则{
NSLog(@“结果%@,错误%@”,结果,错误);
}
}];
//删除:
//dict=self.json;//dict在这里_fbContent,应该用result填充
//[self.delegate performSelector:@selector(didLoadRequest)];
}
@结束
试试这个,希望对你有所帮助:)
#导入
#导入“AppDelegate.h”
@FbApi类;
@协议代理
-(void)didLoadRequestForTableview:(NSMutableDictionary*)aDict;//更改委托方法
@结束
@接口FbApi:NSObject
@属性(赋值,非原子)id委托;
@属性(强,非原子)NSMutableDictionary*json;
-(void)fbIDInformation:(NSString*)fbID to:(NSMutableDictionary*)dict;
@结束
//FbApi.m
#导入“FbApi.h”
@FbApi的实现
-(id)init{
self=[super init];
如果(自我){
}
回归自我;
}
-(void)fbidFormation:(NSString*)fbID to:(NSMutableDictionary*)dict{
NSLog(@“我处于FbApi模式,正在执行新请求”);
NSString*fbquery=[NSString stringWithFormat:@”/%@,fbID];
[FBI]请求连接
startWithGraphPath:fbquery
completionHandler:^(FBRequestConnection*连接,
身份证结果,
N错误*错误){
如果(!错误){
self.json=结果;
[self.delegate didLoadRequestForTableview:self.json];//尝试放置
NSLog(@“fbApi中的结果:%@”,结果);//结果包含内容(!)
}否则{
NSLog(@“结果%@,错误%@”,结果,错误);
}
}];
dict=self.json;//dict在这里_fbContent,应该用result填充
//[self.delegate didLoadRequestForTableview:dict];//更改此(comment this)
}
//在您的位置ViewController.m
@接口位置ViewController()
@结束
@实现位置视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
[自我定位];
[自初始化表视图];
}
-
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 <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;
}
-(void)fbLocation
-(void)fbLocation {
self.fbApi = [[FbApi alloc] init];
self.fbApi.delegate = self;
[self.fbApi fbIDInformation:self.fbID]; //_fbContent is passed, but stays empty...
}
-(void)fbIDInformation:(NSString *)fbID to:(NSMutableDictionary *)dict
-(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);
}
}];
}
- (void) didLoadRequest
- (void) didLoadRequest: (NSMutableDictionary*) data
{
self.fbContent = data;
[self.tableView reloadData];
NSLog(@"This is fbContent form fbApi %@", self.fbContent); //_fbContent = (null)
}
@interface LocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, FbApiDelegate>
...
- (void) didLoadRequest: (NSMutableDictionary*) data;
@end
@interface FbApi : NSObject
...
-(void)fbIDInformation:(NSString *)fbID;
@end