简体   繁体   English

UITableView内的UICollecionView。 如何处理选择?

[英]UICollecionView inside UITableView. How to handle selections?

I am making a form with a tableView with multiple kind of cell types. 我正在用具有多种单元格类型的tableView制作表单。 One of these types contains an UICollectionView with buttons to select some answers. 这些类型之一包含UICollectionView,该UICollectionView具有用于选择一些答案的按钮。

What I need is to be able to hide or show rows in the table, regarding on the answers. 关于答案,我需要的是能够隐藏或显示表中的行。 For example: when answer to question 2 is "No", question 3 is not showing anymore. 例如:当问题2的答案为“否”时,问题3不再显示。

But I don't know how to make that tableview knows what's being selected in one of it's cells 但我不知道如何使该tableview知道在其中一个单元格中正在选择的内容

In the cell that contains the UICollectionView I have this method 在包含UICollectionView的单元格中,我有此方法

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

    let indexPath = optionsCollectionView.indexPathsForSelectedItems?.first
    let cell = collectionView.cellForItem(at: indexPath!) as! OptionsCollectionViewCell
    let data = cell.itemButton.titleLabel?.text
    selectedItem = data

}

But I don't know how to automatically pass it to the tableview so it knows what rows to show or hide... Any idea? 但是我不知道如何自动将其传递给tableview,因此它知道要显示或隐藏哪些行...知道吗?

this is my cellForRowAt 这是我的cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if sortedFixedContentType.count != 0 {
        let item = sortedFixedContentType[indexPath.row]

        switch item.typeId {
        case "42":
            let cell = tableView.dequeueReusableCell(withIdentifier: "FormFileCell", for: indexPath) as! FormFileCell
            return cell;
        case "39":
            let cell = tableView.dequeueReusableCell(withIdentifier: "FormExpenseTypeCell", for: indexPath) as! FormExpenseTypeCell


            return cell;
        case "86":
            let cell = tableView.dequeueReusableCell(withIdentifier: "FormExpensePaymentModeCell", for: indexPath) as! FormExpensePaymentModeCell
            return cell;
        case "87":
            let cell = tableView.dequeueReusableCell(withIdentifier: "FormExpenseFileTypeCell", for: indexPath) as! FormExpenseFileTypeCell

            return cell;
        case "88":
            let cell = tableView.dequeueReusableCell(withIdentifier: "FormProviderCell", for: indexPath) as! FormProviderCell
            return cell;
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! BaseFormCell
            cell.idLabel.text = item.htmlType
            return cell
        }
    }
    else {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! BaseFormCell
        cell.idLabel.text = ""
        return cell
    }
}

Thanks 谢谢

There are Four steps to do this :- 有四个步骤可以做到这一点:

1.First create a protocol lets say 'CustomCellDelegate' in your custom cell where you are using collectionview inside and create a variable that will hold the custom delegate just to give a example suppose your cell name is CustomCell create a CustomCellDelegate and declare it as customDelegate 1,首先创建一个协议,让我们在使用collectionview的自定义单元格中说出'CustomCellDelegate'并创建一个变量来保存自定义委托,仅举一个例子,假设您的单元格名称为CustomCell创建一个CustomCellDelegate并将其声明为customDelegate

protocol CustomCellDelegate : class {
    func Method1()
}

class CustomCell : UITableViewCell {
    var customDelegate : CustomCellDelegate?
}

2.Then you need to fire that delegate from CustomCell class collectionview delegate method didSelectItemAt like this 2.然后您需要从CustomCell类collectionview委托方法didSelectItemAt触发该委托,像这样

extension CustomCell : UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let delegate = customDelegate {
            delegate.Method1()
        }
    }
}

3.Third assign the customDelegate to the view controller where you want to get the delegate for example myViewController here 3.第三次将customDelegate分配给要获取委托的视图控制器,例如myViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customeCellIdentifier", for: indexPath) as! CustomCell
    cell.customDelegate = self // myViewController
    return cell
}

4.Handle the delegate in your view controller like this 4.这样在您的视图控制器中处理委托

extension myViewController : CustomCellDelegate {
    func Method1() {
        print("Method 1 called")
    }
}

I Hope this will solve your problem let me know if you find this answer helpful. 希望这可以解决您的问题,如果您认为此答案有帮助,请告诉我。 Cheers!! 干杯!!

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

相关问题 如何将UITableViewCell放到UITableView的底部。 - How to drop UITableViewCell to bottom of UITableView. UITableView内部的UIView的CA动画。 就像iTunes上的那个一样 - CA animation of a UIView inside a UITableView. Like the one on iTunes iOS:UISearchDisplayController和UITableView。 - IOS: UISearchDisplayController and UITableView. 我想在uitableview中添加第二列。 怎么样? - I would like to add a second column to my uitableview. How? 如何在uiTableView中为每个单元格设置不同的图像。 - How to set different images for every cell in uiTableView.? UITableViewCell 中的平移手势可防止在 UITableView 中滚动。 如何解决? - Pan gesture in UITableViewCell prevents scrolling in UITableView. How to fix it? 我正在尝试查看放置在 UITableView 中的圆角半径。 它对我不起作用 - I am trying for corner radius to view which is placed inside UITableView. It doesn't work for me iPhone UITableView。如何打开像音乐应用程序这样的单字母字母列表? - iPhone UITableView. How do turn on the single letter alphabetical list like the Music App? 如何处理部分瀑布布局 UICollectionView - How to deal with portion waterfall layout UICollecionView UITableView中的UICollectionView。 当集合视图变高时,如何告诉tableview单元改变高度? - UICollectionView within UITableView. How to tell tableview cell to change height when collection view gets taller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM