2016-05-03 5 views
1

私は1つのコレクションビューを持ち、それには5つのセルがあります。私はコレクションビューでセルデータを表示するためにAPIからフェッチしています。ユーザーが任意のセルをクリックすると、そのデータがテーブルビューの下に表示されます。apiからデータをフェッチしながらテーブルビューで最初のデータを表示する方法

今、すべてのものが正常に動作します。しかし、初めて私の見解を読み込むとき。私のテーブルビューにデータが表示されていません...どういう意味ですか?デフォルトでは、コレクションビューセルの最初のデータをtableviewに表示する必要があります。しかし、私は表示できない、または表示できません。

セルだけをクリックすると、テーブルビューにデータが表示されます。しかし、私が必要とするのは、デフォルトではコレクションビューの最初のセルは、その画面を開いたときにテーブルビューで表示する必要があります。

どうすればいいですか?

@IBOutlet var BTCollectionView: UICollectionView! 

    @IBOutlet var DLTableView: UITableView! 

    var BTdata = [BTData]() 

    var Dealsdata = [DealsData]() 
override func viewDidLoad() 
    { 
     super.viewDidLoad() 
// nib for custom cell (table view) 
     let nib = UINib(nibName:"DealsListTableCell", bundle: nil) 
     DLTableView.registerNib(nib, forCellReuseIdentifier: "DealCell") 

     ListBusinessTypes() 
} 
    // Values from Api for Business Types 
    func ListBusinessTypes() 
    { 
     let token = NSUserDefaults.standardUserDefaults().valueForKey("access_token") as! String 

     let headers = ["x-access-token": token] 

     let request = NSMutableURLRequest(URL: NSURL(string: "httpsome url")!, 
              cachePolicy: .UseProtocolCachePolicy, 
              timeoutInterval: 10.0) 
     request.HTTPMethod = "GET" 
     request.allHTTPHeaderFields = headers 

     let session = NSURLSession.sharedSession() 
     let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 
      if (error != nil) 
      { 
       print(error) 

       let ErrorAlert = UIAlertController(title: "Error", message: "Problem with internet connectivity or server, please try after some time", preferredStyle: UIAlertControllerStyle.Alert) 

       // add an action (button) 
       ErrorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

       // show the alert 
       self.presentViewController(ErrorAlert, animated: true, completion: nil) 
      } 
      else 
      { 
       if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject> 
       { 
        let success = json["success"] as? Int 

        if(success == 1) 
        { 
         if let typeValues = json["data"] as? [NSDictionary] 
         { 
          dispatch_async(dispatch_get_main_queue(),{ 

           for item in typeValues 
           { 
            self.BTdata.append(BTData(json:item)) 

            self.BTCollectionView.reloadData() 
           } 
          }) 
         } 
        } 
        else 
        { 
         let message = json["message"] as? String 

         print(message) 

         let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert) 

         // add an action (button) 
         ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

         // show the alert 
         self.presentViewController(ServerAlert, animated: true, completion: nil) 
        } 
       } 
      } 
     }) 

     dataTask.resume() 
    } 

// Mark : Collection View Delegate and Datasource(Business Type) 
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
     return BTdata.count 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    { 
     let cell: DDLCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("HCollectionCell", forIndexPath: indexPath) as! DDLCollectionCell 
     cell.BTName.text = BTdata[indexPath.row].BTNames 
     return cell 
    } 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
    { 
     ListDeals(BTdata[indexPath.row].BTIds!) 
    } 


func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    { 
     return 1 
    } 

    // number of rows 
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return Dealsdata.count 
    } 

    // calling each cell based on tap and users (premium/non premium) 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let tabcell = tableView.dequeueReusableCellWithIdentifier("DealCell") as! DealsListTableCell 

     tabcell.DealName.text = Dealsdata[indexPath.row].DealNames 

     if let imgURL = NSURL(string: Dealsdata[indexPath.row].DealImageUrls!) 
     { 
      let request: NSURLRequest = NSURLRequest(URL: imgURL) 

      let session = NSURLSession.sharedSession() 

      let Imgtask = session.dataTaskWithRequest(request) 
      { 
       (data, response, error) -> Void in 

       if (error == nil && data != nil) 
       { 
        func display_image() 
        { 
         tabcell.DealImage.image = UIImage(data: data!) 
        } 
        dispatch_async(dispatch_get_main_queue(), display_image) 
       } 
      } 
      Imgtask.resume() 
     } 
     else 
     { 
      tabcell.DealImage.image = UIImage(named: "FBLogo") 
     } 

     let formatter = NSDateFormatter() 
     formatter.locale = NSLocale(localeIdentifier: "en_US") 
     formatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSSxxx" 

     let date = formatter.dateFromString(Dealsdata[indexPath.row].DealExpiry!) 

     let dateFormatter = NSDateFormatter() 
     dateFormatter.dateStyle = .MediumStyle 
     dateFormatter.timeStyle = .NoStyle 
     dateFormatter.locale = NSLocale(localeIdentifier: "en_US") 
     let FormattedDate = dateFormatter.stringFromDate(date!) 

     tabcell.RegPriceLabel.text = String(Dealsdata[indexPath.row].DealRegularPrice!) 

     tabcell.SalePriceLabel.text = String(Dealsdata[indexPath.row].DealSalePrice!) 

     tabcell.DealExpiryDate.text = "Expiries on : "+FormattedDate 

     let BArrayValue:NSDictionary = Dealsdata[indexPath.row].DealBusinessDetails! 

     let BName = BArrayValue.valueForKey("business_name") as! String 

     let BImage = BArrayValue.valueForKey("images") as! NSArray 

     let BMainImage = BImage[0] as! NSDictionary 

     let FinalImage = BMainImage.valueForKey("url") as! String 

     if let imgURL2 = NSURL(string: FinalImage) 
     { 
      let request: NSURLRequest = NSURLRequest(URL: imgURL2) 

      let session = NSURLSession.sharedSession() 

      let Imgtask = session.dataTaskWithRequest(request) 
      { 
       (data, response, error) -> Void in 

       if (error == nil && data != nil) 
       { 
        func display_image() 
        { 
         tabcell.DealBusinessImage.image = UIImage(data: data!) 
        } 
        dispatch_async(dispatch_get_main_queue(), display_image) 
       } 
      } 
      Imgtask.resume() 
     } 
     else 
     { 
      tabcell.DealBusinessImage.image = UIImage(named: "FBLogo") 
     } 


     let BLatLng = Dealsdata[indexPath.row].DealCoordinates 

     let UserLocation = CLLocation(latitude: NewCurrentLatitude, longitude: NewCurrentLongitude) 

     let BusinessLocation = CLLocation(latitude: BLatLng![0] as! Double, longitude: BLatLng![1] as! Double) 

     let distance = UserLocation.distanceFromLocation(BusinessLocation)/1000 

     tabcell.DealBusinessNameWithDistance.text = BName+" - "+String(format: "%.1f",distance)+" Km" 

     return tabcell 
    } 

私を助けてください:

は、ここに私のコードです。ありがとう!

私は下の行に任意のものを変更する必要がない:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
    { 
     ListDeals(BTdata[indexPath.row].BTIds!) 
    } 

答えて

0

はあなたのAPIからデータをロードした後ListDeals(BTdata[0].BTIds!)を呼び出すようにしてください。

​​3210
+0

はい、私は最初のデータがあるoccur.That showing.But新しい問題であり、私の画面をロードするとき - data.other細胞は、私が押したときにdata.Butを持っていけないです持つ5 cell.Only最初の二つのセルがあります私の3番目のセル。私の2番目のセルのデータはまだテーブルビューにあります。この問題は私の3,4,5番目のセルでもあります。 – user5513630

関連する問題