简体   繁体   English

如何使用 Swift 在 UICollectionView 中显示/隐藏单元格?

[英]How to show/hide cell in UICollectionView using Swift?

I've displayed cells (0 to 9 and Ok,Cancel button) using UICollectionView.我已经使用 UICollectionView 显示了单元格(0 到 9 和确定,取消按钮)。

Below is what I want:下面是我想要的:

  1. The Ok and Cancel button will be hidden at first.确定和取消按钮将首先被隐藏。
  2. When user selects at least one number then Cancel button becomes visible.当用户选择至少一个数字时,取消按钮变得可见。
  3. When user selects total four number then Ok button also becomes visible.当用户选择总共四个数字时,Ok 按钮也变得可见。

问题截图

Below is the code that I've done :下面是我已经完成的代码:

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)")
    ...
  }

How can this be achieved?如何做到这一点?

hi here is answer with example:嗨,这里是一个例子的答案:

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

}
if "OK" & "CANCEL" both buttons are hidden first , you can use the textfield in which the text is printed by pressing any button . 
 if textfield.text == "" {
  okbutton.ishidden == true
  cancel button.ishdden == true
 }else if textfield.text != "" {
  okbutton.ishidden == false
  cancel button.ishdden == false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM