【问题标题】:How to set a value in completionHandler?如何在completionHandler中设置一个值?
【发布时间】:2015-11-18 09:46:18
【问题描述】:

我有一个关于如何在 completionHandler 块中设置我的 NSArray 的问题。我成功地在我的完成块中为 NSArray 分配了一个值,但是一旦我尝试在完成块之外的 NSLog NSArray 值它给出了 0。

这是我的完整代码。

//
//  HomeTVC.m
//  ABOX
//
//  Created by Zero on 11/18/15.
//  Copyright © 2015 Zero. All rights reserved.
//

#import "HomeTVC.h"
#import "facebook.h"

@interface HomeTVC ()<UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary *userPageLikesParams;
    NSArray *pagesInfo;
    facebook *fb;
}
@end

@implementation HomeTVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    fb = [[facebook alloc] init];

    userPageLikesParams = @{@"fields": @"about,name,created_time",@"limit": @"5"} ;
    [fb getUserPagelikes:userPageLikesParams completionHandler:^(NSDictionary *pagesResult) {

        pagesInfo = [[NSArray alloc] init];
        pagesInfo = pagesResult[@"data"];

        // NSArray *p = pagesResult[@"data"];
        // [self pages:p];
    }];
}

//- (void)pages:(NSArray*)pageInfo {
//    
//    self.pagesInfo = pageInfo;
//    NSLog(@"fb pages result ayam : %@", self.pagesInfo[0]);
//    NSLog(@"array counzz : %d", (int)self.pagesInfo.count);
//}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 // here I got 0, how can I solve this problem?
    NSLog(@"table count : %d", (int)pagesInfo.count);
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

@end

提前致谢。

【问题讨论】:

  • 确保在完成块完成后记录数组
  • 为什么要创建一个空数组,然后在下一条语句中将其丢弃?
  • 通过声明 __block NSArray *pagesInfo; 进行检查而不是 NSArray *pagesInfo;
  • 当页面信息数组有值时,只需在“getUserPagelikes”块中重新加载您的表格视图
  • 谢谢大家,现在它可以按照我的意愿完美运行。 duhhh 因为愚蠢的错误,我浪费了 3 个小时的时间。 :(

标签: ios objective-c iphone facebook uitableview


【解决方案1】:

在完成块完成后重新加载您的表格视图,您应该会看到您的 pagesInfo 数组已填充并返回一个计数。

【讨论】:

  • 在返回数组的计数之前,您永远不必进行 nil 检查。如果数组引用为零,则[array count] 的返回为零。
【解决方案2】:

根据@michal,您应该像这样重新加载下面的 tableView:

- (void)viewDidLoad 
{
  [super viewDidLoad];
  self.tableView.delegate = self;
  self.tableView.dataSource = self;
  fb = [[facebook alloc] init];
  userPageLikesParams = @{@"fields": @"about,name,created_time",@"limit": @"5"} ;
  [fb getUserPagelikes:userPageLikesParams completionHandler:^(NSDictionary *pagesResult) 
  {
    pagesInfo = pagesResult[@"data"];
    [yourTableViewObj reloadData];
    // NSArray *p = pagesResult[@"data"];
    // [self pages:p];
  }];
}

希望它会有所帮助。

【讨论】:

  • 他不需要“pagesInfo = [[NSArray alloc] init];”行,因为它会在下一行被重写/重置。
猜你喜欢
  • 2010-11-17
  • 2013-08-08
  • 2020-05-19
  • 2016-10-22
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多