2016-04-28 7 views
0

私は1つのUICollectionViewを持っています。私は自分のコレクションビューのセルをURLから動的に表示しています。私はセルの幅と高さを設定して、4つのセルだけが1行になるようにします。だから今私のURLから、私は完全に14項目を取得しています。私はそのラベルの14ラベル名と14イメージを意味します。テーブルビューには14項目が表示されなければなりません。コレクションビューのセルでcountが8より大きい場合、余分なセルを追加します。

私が必要としているのは、コレクションビューに2行しか表示する必要がなく、セルの幅と高さを設定します。行ごとに4つのアイテムが意味するので、2つの行は完全に8つのアイテムを意味します。しかし、私のURLから、私は14項目を得ていますか?

私が必要とするのは、アイテム数が7以上の場合です。フォーム0〜7をカウントしています。次に、7番目のセルを静的なように表示する必要があります。テキストは自動的に「もっと見る"

どうすればよいですか?それは、UIエディタでの細胞の他のタイプを追加(それを言うSeeMoreを名前を付け、および変更を超える8.

return BTdata.count>8 ? 8 : BTdata.count; 

次の場合

import UIKit 

class HomeViewController: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate { 


    @IBOutlet weak var collectionView1: UICollectionView! 


    var BTdata = [BTData]() 


    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     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: “some 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.collectionView1!.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() 
    } 

     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    { 
      return BTdata.count 

    } 


    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell 
    { 

      let cell: collview1 = collectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as! collview1 
      cell.lblCellA.text = BTdata[indexPath.row].BTNames 

      cell.imgCellA.image = UIImage(named: tableImages[indexPath.row]) 


      return cell 


    } 




     collection view cell space and size 
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 
    { 


      return CGSizeMake((self.view.frame.size.width/4) - 10, (self.view.frame.size.width/4) - 15); 

    } 



} 

答えて

1

まずBTdata.countが、8を返しません。コレクションビュー項目ハンドラは、最後の行のために適切なもの返すように:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if indexPath.row==7 { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("SeeMore", forIndexPath: indexPath) 
    return cell  
    } else { 
    let cell: collview1 = collectionView.dequeueReusableCellWithReuseIdentifier("Cell1", forIndexPath: indexPath) as! collview1 
    cell.lblCellA.text = BTdata[indexPath.row].BTNames 
    cell.imgCellA.image = UIImage(named: tableImages[indexPath.row]) 
    return cell 
    } 
} 

およびに応じて、そのセルの適切なサイズを返します方法。

+0

'return BTdata.count> 8?BTdata.count; 'この行はオプションの値の後にカンマを追加するように要求していますか? – user5513630

+0

だから、もっと見るためにもう1つのcell.xibを作成する必要があります。条件が満たされていれば、そのセルを呼び出す必要があります。もしindexpath.rpw == 7.Amなら、私は正しいでしょうか? – user5513630

+0

これは私が提案したものです。 –