1

我正在加载带有 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
4

2 回答 2

2

试试这样:

代替

-(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)
}

在 LocationViewController.h 中:

@interface LocationViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, FbApiDelegate>

...

- (void) didLoadRequest: (NSMutableDictionary*) data;

@end

FbApi.h:

@interface FbApi : NSObject
...
-(void)fbIDInformation:(NSString *)fbID;

@end
于 2013-08-02T11:04:27.450 回答
0

试试这个,希望对你有帮助.. :)

   #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;
  }


于 2013-08-02T10:06:14.597 回答