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

标签 ios swift uicollectionview uicollectionviewcell

我已经使用UICollectionView显示了单元格(0到9和“确定,取消”按钮)。

以下是我想要的:

  • 首先将隐藏“确定”和“取消”按钮。
  • 当用户选择至少一个数字时,“取消”按钮将变为可见。
  • 当用户选择总共四个数字时,“确定”按钮也将变为可见。

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

    如何做到这一点?

    最佳答案

    您好,这里是示例答案:

    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
    
    }
    

    关于ios - 如何使用Swift在UICollectionView中显示/隐藏单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238942/

    相关文章:

    iphone - 协助 iOS 无线应用程序分发的库/服务

    ios - 在 iOS 上的 Swift 中使用 Realm DB 对应用程序大小有什么影响

    swift - NSCalendar 的 calendar.rangeOfUnit 函数中的错误?

    iOS Swift : How to build two different . ipa 用于同一项目的不同国家

    ios - 使 UICollectionViewController 分页部分

    ios - 是否可以在没有 Apple Developer Program 的情况下在 iOS 应用程序中使用 Firebase Cloud Messaging?

    iphone - Youtube视频无法在iOS6应用上播放,但从App Store下载后可以正常播放

    ios - AVAudioEngine中AuAudioBuffer的回调实现

    swift - NSInternalInconsistencyException',原因 : 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForItemAtIndexPath, 自定义布局

    Swift - UICollectionView 重复(复制)单元格