0

我无法加载包含多个部分的 UITableView。为了填充它,我使用了一个函数(从 Twitter 获取提要)。在视图加载的那一刻,该函数为其字段返回 NULL 值,但几秒钟后它返回所需的提要。

但是,在返回所需的提要之前,我的 tableView 中的字段显示为 NULL,然后它们刷新并正确填充(无 NULL 值)。

我的问题是,在正确加载提要之前,如何使 tableView 单元格不加载?

我的 Facebook 提要也有同样的问题,但是它崩溃了,因为它甚至没有返回任何值。

在 ViewDidLoad 我放了

[self getTwitterFeed:^() { 

    [self.tableView reloadData];
}];

编辑这里是方法的代码

- (void)getTwitterFeed:(void (^)(void))completion {
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account
                              accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
@try
{
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"TwitterLoggedIn"] isEqualToString:@"YES"]) {

        [account requestAccessToAccountsWithType:accountType
                                         options:nil completion:^(BOOL granted, NSError *error)
         {
             if (granted == YES)
             {
                 NSArray *arrayOfAccounts = [account
                                             accountsWithAccountType:accountType];

                 if ([arrayOfAccounts count] > 0)
                 {
                     ACAccount *twitterAccount = [arrayOfAccounts objectAtIndex:[[NSUserDefaults standardUserDefaults] integerForKey:@"TwitterAccountNumber" ]];

                     NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/home_timeline.json"];

                     NSMutableDictionary *parameters =
                     [[NSMutableDictionary alloc] init];
                     [parameters setObject:@"35" forKey:@"count"];
                     [parameters setObject:@"true" forKey:@"include_entities"];


                     SLRequest *postRequest = [SLRequest
                                               requestForServiceType:SLServiceTypeTwitter
                                               requestMethod:SLRequestMethodGET
                                               URL:requestURL parameters:parameters];

                     postRequest.account = twitterAccount;

                     [postRequest performRequestWithHandler:
                      ^(NSData *responseData, NSHTTPURLResponse
                        *urlResponse, NSError *error)
                      {
                          self.dataSource = [NSJSONSerialization
                                             JSONObjectWithData:responseData
                                             options:NSJSONReadingMutableLeaves
                                             error:&error];

                          if (self.dataSource.count != 0) {
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  NSLog(@"Description %@",_dataSource);
                                  for(int i=0;i<[_dataSource count];i++)
                                  {
                                      NSMutableString *url = [NSMutableString stringWithFormat: @"https://www.twitter.com/%@/status/%@",[[[_dataSource objectAtIndex:i ]objectForKey:@"user"] valueForKey:@"screen_name"],[[_dataSource objectAtIndex:i ]objectForKey:@"id"]];

                                      [tweetURL addObject:url];

                                      NSMutableString *urlApp = [NSMutableString stringWithFormat: @"twitter://user?screen_name=%@?status?id=%@",[[[_dataSource objectAtIndex:i ]objectForKey:@"user"] valueForKey:@"screen_name"],[[_dataSource objectAtIndex:i ]objectForKey:@"id"]];

                                      [tweetAppURL addObject:urlApp];
                                  }

                                  CGRect frame = CGRectMake (120, 120, 80, 80);
                                  activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: frame];
                                  activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
                                  activityIndicator.color = [UIColor whiteColor];
                                  [activityIndicator startAnimating];
                                  activityIndicator.hidesWhenStopped=YES;
                                  [self.view addSubview:activityIndicator];



                             completion();

                                  //[self.tableView reloadData];
                              });

                          }
                      }];
                 }
             } else {

             }  

         }];
    }


    else  //IF FEED IS NOT TURNED ON
    {

        [self.tableView reloadData];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your TWITTER feed is either turned of or isn't initiated!" message:@"Please enable it in Settings" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

         }

@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}

}

4

2 回答 2

0

尝试这样的事情

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do something perhaps

    // Do any additional setup after loading the view, typically from a nib.
    [self getTwitterFeed:^() { // Completion block
        dispatch_async(dispatch_get_main_queue(), ^{
                           [myTableView reloadData];
                       });
    }];
}


- (void)getTwitterFeed:(void (^)(void))completion {
    // Get the feed and call:
    NSLog(@"We finished receiving the data");
    completion();
}

并显示正确的行数(需要根据#sections进行编辑)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return (self.myFeedArray.count == 0 ? 0 : self.myFeedArray.count);
}

加载单元

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(myFeedArray.count == 0) { // no feeds yet
        NSLog(@"The count in the table is 0");
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.textLabel.text = @"Updating...";
        [cell.textLabel setTextAlignment:NSTextAlignmentCenter];
        [cell.textLabel setAlpha:0.5];
        cell.userInteractionEnabled = NO;
        return cell;
    }

    //else do stuff

} 
于 2013-06-24T10:08:37.983 回答
0
- (void)viewDidLoad
{
   [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSData * data=[NSData dataWithContentsOfURL:[NSURL URLWithString:URLForPayload]];
    [self performSelectorOnMainThread:@selector(fetchCompleted:) withObject:data waitUntilDone:YES];
   });
}



-(void) fetchCompleted:(NSData *)responseData
 {


   // Complete data available now reload TableView

 }
于 2013-06-24T10:13:26.963 回答