想要在swift3中创建集合视图
我想用可展开的格式创建uicollectionview。 当我们点击可扩展的单元格时,应该加载更多单元格的单元格。再次敲打后关闭该部分。 这就像展开和折叠部分。 部分细胞和内部细胞都是动态的。想要在swift3中创建集合视图
你将不得不这样做。
在全局构建这样的结构,以便您可以在//表格方法中访问这些结构。
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
@IBOutlet var tblView: UITableView!
let arr = [ "dict1":["1":"one","2":"two"],
"dict2":["3":"three","2":"four"],
] as [String : Any]
var arrDict = [[String:AnyObject]]()
var aDict1 = [String:Any]()
var aDict2 = [String:Any]()
var aDict3 = [String:Any]()
var isbuttonTappedForExpand = false
override func viewDidLoad() {
super.viewDidLoad()
aDict1 = ["Tittle": "Quick Start",
"RowKey": "0",
"ValueKey": ["Section0 row0",
"Section0 row1",
"Section0 row2",
]
] as [String : Any]
aDict2 = ["Tittle": "Personal",
"RowKey": "0",
"ValueKey": ["Section0 row1",
"Section1 row1",
"Section1 row2",
]
] as [String : Any]
aDict3 = ["Tittle": "Business",
"RowKey": "0",
"ValueKey": ["Section0 row2",
"Section2 row1",
"Section2 row2",
]
] as [String : Any]
arrDict.append(aDict1 as [String : AnyObject])
arrDict.append(aDict2 as [String : AnyObject])
arrDict.append(aDict3 as [String : AnyObject])
print("Array of dictionaries is\(arrDict)")
print([arr ])
print(arrDict[0]["Tittle"] ?? 2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//Then pass your data into tableview delegate methods
func numberOfSections(in tableView: UITableView) -> Int {
return arrDict.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (arrDict[section]["RowKey"] as? String == "0"){
return 0
}
else{
return arrDict.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell:CustomTableViewCell = tableView .dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
let arr = arrDict[indexPath.row]["ValueKey"] as! [String]
cell.btnClickAddMore.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat{
return 100
}
//you will have to add sections first after clicking on sections you will get your cell so here you will make your view for section
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor .black
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let rect = CGRect(x: 0, y: 0, width: tblView.frame.size.width, height: 100)
let headerView = UIView(frame:rect)
headerView.backgroundColor = .blue
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SectionView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! SectionViewClass
view.btnClickAdd.backgroundColor = .blue
view.btnClickAdd.tag = section
view.btnClickAdd.addTarget(self, action: #selector(pressButton(button:)), for: .touchUpInside)
view.frame = headerView.bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
headerView.addSubview(view)
return headerView
}
func pressButton(button: UIButton) {
if (arrDict[button.tag]["RowKey"] as? String == "0") {
arrDict[button.tag]["RowKey"] = "1" as AnyObject?
}
else if (arrDict[button.tag]["RowKey"] as? String == "1") {
arrDict[button.tag]["RowKey"] = "0" as AnyObject?
}
tblView .reloadData()
}
}
看来你的解决方案是针对UITableView的。我想要这个UICollectionView。 –
你可以为collectionview.main做同样的事情,这里是结构。你可以在这里使用集合视图方法而不是uitableview数据源方法。如果你需要,我可以为你提供一个演示。 –
是的,请给 –
请这些 https://github.com/apploft/APLExpandableCollectionView 而 https://www.raywenderlich.com/99087/swift-expanding-cells-ios-collection-views – Rivendell
这是UITableView我想在UICollectionView中做到这一点 –
你检查了这些例子吗?他们档案! – Rivendell