简体   繁体   English

当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器?

[英]How to move another view controller when i tapped any collection view cell (without taping last cell)?

How to move another view controller when i tapped any collection view cell (without taping last cell) ? 当我轻按任何集合视图单元格时(不分录最后一个单元格),如何移动另一个视图控制器? I don't know how to fix this issue, i am a beginner in iOS App Development. 我不知道如何解决此问题,我是iOS App开发的初学者。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return addCategory.count + 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cellID = indexPath.row < addCategory.count ? "CategoryCell" : "ExtraCell"

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
    setupCell(cell: cell, indexPath: indexPath, type: cellID)

    return cell
}

func setupCell(cell: UICollectionViewCell, indexPath: IndexPath, type: String) {

    switch(type) {
    case "CategoryCell": setupCategoryCell(cell: cell as! CategoryCollectionCell, indexPath: indexPath)
    case "ExtraCell": setupAddButtonCell(cell: cell as! CategoryExtraCell, indexPath: indexPath)
    default: break
    }
}

func setupCategoryCell(cell: CategoryCollectionCell, indexPath: IndexPath) {

    let cat = addCategory[indexPath.row]
    cell.lblHeader.text = cat.category_name

    //cell.lblHeader.text = arrHeader[indexPath.row]
    //cell.lblSubHeader.text = arrSubHeader[indexPath.row]
}

func setupAddButtonCell(cell: CategoryExtraCell, indexPath: IndexPath) {
    //Extra Button "Add Button" in a cell
}

When I click on any cell (without tapping last cell), then it will not move to another view controller, the app crashes and doesn't show any error. 当我单击任何单元格(不点击最后一个单元格)时,它将不会移动到另一个视图控制器,该应用程序将崩溃并且不会显示任何错误。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    self.collCategory.allowsMultipleSelection = false

    if indexPath.row < addCategory.count {

        print("Main Cell")

        for i in 0..<addCategory.count {

            if indexPath.row == i {
                let categoryTaskVC = storyboard?.instantiateViewController(withIdentifier: "CategoryTaskVC") as! CategoryTaskVC
                self.navigationController?.pushViewController(categoryTaskVC, animated: true)

                //let cat = addCategory[i]
                //categoryTaskVC.lblHeader.text = cat.category_name
                print("OK")
            } else {
                print("error")
            }
        }

    } else {
        print("Add New Cell")

        self.blurEffects()
        view.addSubview(blurEffectView)

        //Alert View Controller when Adding Categories...
        let inputBox = BMInputBox.boxWithStyle(.plainTextInput)
        inputBox.blurEffectStyle = .extraLight

        inputBox.title = NSLocalizedString("Add Category", comment: "")
        inputBox.message = NSLocalizedString("Please enter unique category name.", comment: "")

        inputBox.customiseInputElement = {(element: UITextField) in
            element.placeholder = "Enter a category"
            return element
        }

        inputBox.submitButtonText = NSLocalizedString("OK", comment: "")

        inputBox.onSubmit = {(value: String...) in

            //Store value in text field in "text" object.
            for text in value {

                let strCategory = text
                print("STR CATE : \(strCategory)")

                DispatchQueue.main.async(execute: {

                    //Store category in CoreData
                    categoryCoreData.saveData(tfCat: strCategory)

                    //Fetch Category Data
                    categoryCoreData.fetchData()
                    self.collCategory.reloadData()
                })
            }

            self.blurEffectView.removeFromSuperview()
        }

        inputBox.cancelButtonText = NSLocalizedString("Cancel", comment: "")

        inputBox.onCancel = {
            //Remove blur effects from Superview
            self.blurEffectView.removeFromSuperview()
        }

        inputBox.show()
    }
}

Console 安慰

App Screen 应用画面

Xcode Xcode

Change the if branch to this: if分支更改为此:

if indexPath.row < addCategory.count - 1 {

    print("Main Cell")

    let categoryTaskVC = storyboard?.instantiateViewController(withIdentifier: "CategoryTaskVC") as! CategoryTaskVC
    self.navigationController?.pushViewController(categoryTaskVC, animated: true)
    let cat = addCategory[indexPath.row]
    categoryTaskVC.lblHeader.text = cat.category_name
}

暂无
暂无

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

相关问题 双击集合视图单元格时如何使图像可见? - How do I make image visible when collection view cell is double tapped? 如何将集合视图单元格的indexPath传递给另一个视图控制器 - How to pass a collection view cell's indexPath to another view controller 如何在不使用情节提要或IB的情况下从嵌入式集合视图的单元导航到另一个视图控制器? - How to navigate from an embedded collection view’s cell to another view controller without using Storyboard or IB? 在swift 4.0中点击收集视图单元格时更改标签的文本 - Changing the text of a label when a collection view cell is tapped in swift 4.0 点击时无法为选定的集合视图单元格设置动画 - Trouble animating the selected collection view cell when tapped 如何将选定的集合视图单元格的图像推送到另一个视图控制器? - How Can I push the image of selected Collection View Cell to another View Controller? 如何将数据从集合视图单元传递到新的视图控制器(以编程方式且没有情节提要)? - How can I passed data from collection view cell to new view controller (programmatically and without storyboard)? 如何找出在集合视图单元格中挖掘的集合视图单元格 - How to find out which collection view cell is tapped inside collection view cell 如何基于所点击的单元格在视图控制器中更改数据 - How to change data one a view controller based on the cell tapped 轻按另一个时在表格视图单元格中取消选择一个按钮 - Deselect a button in a table view cell when another is tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM