2016-07-07 143 views
1

我已经使用UICollectionView显示了单元格(0到9和确定,取消按钮)。如何使用Swift在UICollectionView中显示/隐藏单元格?

下面是我想要的东西:

  1. 确定和取消按钮,将首先被隐藏。
  2. 当用户选择至少一个号码时,取消按钮变得可见。
  3. 当用户选择总共四个号码,然后确定按钮也变得可见。

Screenshot for problem

下面是我做的代码:

var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "Cancel","0", "OK"] 

... 

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

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCollectionViewCell 

     cell.lblNumber!.text = self.items[indexPath.item] 

     if (self.items[indexPath.item])=="Cancel" { 
      cell.hidden = true; 
     } 

     if (self.items[indexPath.item])=="OK" { 
      cell.hidden = true; 
     } 

     return cell 
    } 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

    print("You selected cell #\(indexPath.item) and value : \(self.items[indexPath.item]) count : \(counter)") 
    ... 
    } 

如何才能实现这一目标?这里

+0

保存在cellForItemAtIndexPath方法将取消和OK键细胞引用。您可以将其'contentView'设置为隐藏。然后在didSelectItemAtIndexPath中根据所选项目的数量,可以使用已保存单元格的引用来隐藏/取消隐藏OK/Cancel按钮的contentView。 – 7vikram7

+1

另一种方法是保持选择列表(在'didSelect..'上)并强制更新(通过'reloadData()'),根据列表的数量启用/禁用取消/ OK。顺便说一下,imo启用/禁用按钮对用户来说要比显示/隐藏更加直观。 – Alladinian

回答

0

喜是例子回答:

import UIKit 

class ViewController: UIViewController { 

    var objectNumCollectionViewCell : NumCollectionViewCell = NumCollectionViewCell() 

    @IBOutlet weak var lblnumber: UILabel! 
    var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "Cancel","0", "OK"] 

    var strnum: String = "" 

    @IBOutlet weak var collectionviewMain: UICollectionView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - CollectionView DataSource Method 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return 1 
    } 

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
      objectNumCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! NumCollectionViewCell 
      objectNumCollectionViewCell.lblNum.text = items[indexPath.item] as String 

     if indexPath.item == 9 { 
      if lblnumber.text?.characters.count > 0 { 
       objectNumCollectionViewCell.hidden = false 
      } 
      else{ 
       objectNumCollectionViewCell.hidden = true 
      } 
     } 
     else 
     { 
      objectNumCollectionViewCell.hidden = false 
     } 

     if indexPath.item == 11 { 
      if strnum.characters.count > 3 { 
       objectNumCollectionViewCell.hidden = false 
      } 
      else{ 
       objectNumCollectionViewCell.hidden = true 
      } 
     } 

     objectNumCollectionViewCell.layer.borderWidth = 1.0 
     objectNumCollectionViewCell.layer.borderColor = UIColor.darkGrayColor().CGColor 
     objectNumCollectionViewCell.layer.cornerRadius = 10.0 
     objectNumCollectionViewCell.layer.masksToBounds = true 

     return objectNumCollectionViewCell 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets{ 
      return UIEdgeInsetsMake(0, 5, 0, 5); 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{ 
     return CGSizeMake(self.view.frame.size.width/3-10, 100) 
    } 

    // MARK: - CollectionView Delegate Method 

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath){ 

     objectNumCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! NumCollectionViewCell 

     if indexPath.item == 9 { 
      strnum.removeAtIndex(strnum.endIndex.predecessor()) 
     } 
     else if indexPath.item == 11{ 
      let alert:UIAlertView = UIAlertView(title: "Number Demo", message: "You have Pressed Ok", delegate: nil, cancelButtonTitle: "ok") 

      dispatch_async(dispatch_get_main_queue(), { 
       alert.show() 
      }) 
     } 
     else 
     { 
      if strnum.characters.count < 4 { 
       strnum.append(Character(items[indexPath.item] as String)) 
      } 
     } 

     lblnumber.text = strnum 
     collectionviewMain.reloadData() 
    } 
} 

// Custom cell class 
// identifier = "cell" 

import UIKit 

class NumCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var lblNum: UILabel! // please declare label in storyboard 

} 
相关问题