gpt4 book ai didi

ios - 当我点击任何 Collection View 单元格(不点击最后一个单元格)时,如何移动另一个 View Controller ?

转载 作者:行者123 更新时间:2023-11-30 11:47:39 24 4
gpt4 key购买 nike

当我点击任何 Collection View 单元格(不点击最后一个单元格)时,如何移动另一个 View Controller ?我不知道如何解决这个问题,我是 iOS 应用程序开发的初学者。

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
}

当我单击任何单元格(不点击最后一个单元格)时,它不会移动到另一个 View Controller ,应用程序崩溃并且不显示任何错误。

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

最佳答案

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
}

关于ios - 当我点击任何 Collection View 单元格(不点击最后一个单元格)时,如何移动另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com