2016-10-12 3 views
0

私は現在、私の解析サーバに格納された情報(ユーザー名、説明、プロフィール画像)現在ロードテーブル

...とのtableViewを投入しようとしているを表示する前にパースからのすべてのデータを、私は私がテーブルビューを開くと、情報を表示することはできませんが、戻るをクリックしてテーブルを開くと、すべての情報が表示され、その理由が分かりません。

Aは、最初の解析から必要なすべての情報を取得し、そのような配列にそれらを格納します。

let query = PFQuery(className: institutionTitle) 
    query.findObjectsInBackground { 
     (objects, error) in 
     if error == nil { 
      // The find succeeded. 
      // Do something with the found objects 
      if let objects = objects { 
       for object in objects { 
        self.postGrabber.append(object) 
        self.pSenderUsername.append(object["sender"] as! String) 
        self.pPostBody.append(object["textPost"] as! String) 
       } 
      } 
     } else { 
      // Log details of the failure 
      print("Error: \(error!)") 
     } 
    } 

私は現在viewDidLoad()viewWillAppear()私は、デリゲートとデータソースを設定

reloadData()の両方を使用していますテーブルビューの viewDidLoad()機能ですべてのデータが正常にロードされたとされるまで

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell: UniversityFeedCellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! UniversityFeedCellTableViewCell 

    cell.sender.text = pSenderUsername[indexPath.section] 
    cell.senderPost.text = pPostBody[indexPath.section] 
    cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width/2 
    cell.profilePicture.layer.borderColor = UIColor.orange.cgColor 
    cell.backgroundColor = UIColor.white 

    return cell 
} 

私は、ユーザーの待ち時間になるだろう方法についていくつかの情報を探しています:

私は、テーブルの上に各セルを設定するためにcellForRowAtIndexPathメソッドを使用します人口が多い。助けていただければ幸いです。

ありがとうございました。私自身より多くの情報をリクエストしてください。

+0

これは主にテーブルに読み込まれていない画像です。 –

答えて

0

を聞く...何かをこのようにトリックを行う必要があります。

let query = PFQuery(className: institutionTitle) 
query.findObjectsInBackground { 
    (objects, error) in 
    if error == nil { 
     // The find succeeded. 
     // Do something with the found objects 
     if let objects = objects { 
      for object in objects { 
       self.postGrabber.append(object) 
       self.pSenderUsername.append(object["sender"] as! String) 
       self.pPostBody.append(object["textPost"] as! String) 
      } 
       dispatch_async(dispatch_get_main_queue(), { 
        self.tableView.reloadData() // Add this line and it should work 
       }) 
     } 
    } else { 
     // Log details of the failure 
     print("Error: \(error!)") 
    } 
} 

、あなたのデータをロードするたびにしたい場合は、あなたのビューコントローラを使用して、データに初回のみをロードする必要があるならば、その後のviewDidLoadで、そうでない場合、)(viewWillAppearでそれを行う表示されます()

+0

ありがとう!私はあなたのフェッチクエリをviewDidLoadメソッドまたはviewWillAppearメソッドに置くべきでしょうか? –

+0

ベストプラクティスは、直接呼び出すのではなくコールバックブロックを実装することです –

0

サーバからjsonを取得するときにコールバックメソッドを設定する必要があります。その後、結果をフェッチする場合は、リロードメソッドを呼び出して、インジケータを設定することができます。そのuはいくつかのコード例が必要な場合は、私は、コードを挙げることができるデータに

をリロードしますフェッチで行わコールバックメソッドを使用すると、すべてのサーバーから取得した後、あなたは、テーブルビューのデータをリロードする必要が

+0

データをダウンロードして配列に格納する方法を説明するために質問を更新しました。これが大きな違いをもたらすかどうかは分かりません。 –

+0

私はObjective Cでの経験があります。私はSwiftにない客観的なCでコードを共有しても構わないと思います。後者では迅速にコードをカスタマイズすることができます –

+0

確かにそれは素晴らしいでしょう。 –

0

まずは.Mファイルがこのメソッドを作成した後

+(BOOL)CallService3:(NSString*)serviceName postData:(NSString*)params qString:(NSString*)QueryStr callBackBlock:(void (^)(id response))responeBlock{ 


    NSString *jsonString =params;//if you have dictionary then [params JSONRepresentation]; 
    NSString *post = [NSString stringWithFormat:@"json=%@", jsonString]; 


    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO]; 


    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    sessionConfiguration.HTTPAdditionalHeaders = @{ 
                @"api-key"  : @"API_KEY" 
                }; 

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration]; 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseURLS,serviceName]]; 
    NSLog(@"url %@",[url absoluteString]); 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    request.HTTPMethod = @"GET"; 
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 

     NSDictionary* jsonObject = [NSJSONSerialization JSONObjectWithData:data 
                    options:kNilOptions 
                    error:&error]; 


     responeBlock(jsonObject); 
    }]; 
    [postDataTask resume]; 

    return TRUE; 


} 

をウェブAPIやWebサービスを呼び出すためのメソッドを作成する意味あなたの中に実装ファイル

void (^successCallback)(id response); 
    void (^failCallback)(NSError *error); 
+(BOOL)CallService3:(NSString*)serviceName postData:(NSString*)params qString:(NSString*)QueryStr callBackBlock:(void (^)(id response))responeBlock; 

.hファイル//コールバック・ブロックあなたにこのコールバックのブロックを追加する必要がありますコールバック関数のためにこれを呼び出さなければなりません

-(void)callWebApi{ 
    //here you can call indicator if you need this class i can share with you 
     // [self startActivityIndicatorInView:self.view withMessage:@"please wait"]; 

     [Connection CallService3:WebApi_Category postData:jsonstring qString:str callBackBlock:^(id response){ 
        // NSLog(@"param .. %@",response); 
        // NSLog(@"%@",[response allKeys]); 
        //NSLog(@"param .. %@",response); 

        jsonArray=[[NSDictionary alloc]initWithDictionary:response]; 
        dispatch_async(dispatch_get_main_queue(), ^(void){ 
    //stop indicator here 
         // [self stopActivityIndicatorInView:self.view]; 
        [self call_your_method_populating_table:[jsonArray objectForKey:kModel]]; 
//now call reload tableview 

[_tableview reloadData]; 
        }); 
       }]; 


    } 
関連する問題