【发布时间】:2017-03-22 08:52:59
【问题描述】:
我是 Swift 新手,我的 CollectionView 有问题。我在 CollectionView 中有 2 个自定义单元格。第一个单元格,比如cell 和第二个cellOcc,其中cell 是空表,cellOcc 是占用表。应用程序启动时它工作正常,但是当我尝试搜索它们时它变得很奇怪(内容不在它们的位置正确)。以下是我的代码,任何建议和帮助都会对我有所帮助。提前致谢。
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filteredSearchDataTable = filterArray(filteredDataTable, keywords: "tischnr", searchStr: searchText)
tableCollectionView.reloadData()
}
..........
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var count:Int = 0
if(searchBarIsActive) {
count = filteredSearchDataTable.count
}
else{
count = filteredDataTable.count
}
return count
// return max(filteredDataTable.count, 2)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell: TableCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "emptyTable", for: indexPath) as! TableCollectionViewCell
cell.layer.borderWidth = 1
cell.layer.cornerRadius = 5
if (searchBarIsActive) {
currTable = (filteredSearchDataTable[indexPath.row] as! [AnyHashable: Any])
cell.labelTableNr.text = (String(currTable["tischnr"] as! Int))
}
else {
currTable = (filteredDataTable[indexPath.row] as! [AnyHashable: Any])
cell.labelTableNr.text = (String(currTable["tischnr"] as! Int))
currRechnr = getJSONArrayIndex(filteredDataBill, name: "tischnr", o: ((currTable["tischnr"] as! Int)))
if self.selectedIndex.index(of: indexPath as NSIndexPath) == nil {
//
} else {
//
}
}
var rechnrValue = 0
if currRechnr >= 0 {
rechnrValue = ((filteredDataBill[currRechnr] as! [AnyHashable: Any])["rechnr"] as! Int)
if (rechnrValue != 0) {
let cellOcc : TableOccupiedCollectionViewCell = tableCollectionView.dequeueReusableCell(withReuseIdentifier: "occupiedTable", for: indexPath) as! TableOccupiedCollectionViewCell
cellOcc.layer.borderWidth = 1
cellOcc.layer.cornerRadius = 5
cellOcc.tableNrOccLabel.text = ((filteredDataTable[indexPath.row] as! [AnyHashable: Any]) ["bezeich"] as! String)
cellOcc.paxOccLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["belegung"] as! Int))
cellOcc.guestNameLabel.text = ((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["bilname"] as! String)
cellOcc.amountLabel.text = (String((filteredDataBill[currRechnr] as! [AnyHashable: Any]) ["saldo"] as! Int))
return cellOcc
}
else {
return cell
}
}
return cell
}
【问题讨论】:
标签: ios swift3 uicollectionview