[英]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.