我試圖實現一項功能,其中如果用戶單擊我的UICollectionView
(即嵌入在UITableViewCell
中)中的某個項目,則會導致另一部分出現在UITableView
(UICollectionView
下方)與關於那個項目的信息。我遇到單擊時出現的部分高度問題。如果我最初在heightForRowAtIndexPath中將其設置爲0,那麼稍後似乎沒有辦法改變單元格高度。我嘗試給單元格初始高度,然後用cell.hidden
隱藏它,但仍然可以看到該部分。也許有另一種方式來做到這一點,但經過大量的谷歌搜索,我很快就沒有了。隱藏和展開UITableView部分
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 3 {
return 90
}
else if indexPath.section == 1 {
return 185
}
else if indexPath.section == 2 {
return 155
}
else if indexPath.section == 4 {
return 100
}
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
}
else if indexPath.section == 3 {
let cell = tableView.dequeueReusableCellWithIdentifier("weeklyCell", forIndexPath: indexPath) as! WxTableViewCell
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.forecastCollectionView.delegate = self
cell.forecastCollectionView.dataSource = self
cell.forecastCollectionView.reloadData()
cell.forecastCollectionView.tag = indexPath.row
cell.forecastCollectionView.showsHorizontalScrollIndicator = false
return cell
}
else if indexPath.section == 4 {
let cell = tableView.dequeueReusableCellWithIdentifier("dailyInformation", forIndexPath: indexPath) as! WxTableViewCell
cell.hidden = true
cell.contentView.hidden = true
return cell
}
...
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let index = NSIndexPath(forRow: 0, inSection: 3)
let myindex = NSIndexPath(forRow: 0, inSection: 4)
var myTable = self.tableView
var lastCell = myTable.cellForRowAtIndexPath(index)
var dailyCell = myTable.cellForRowAtIndexPath(myindex)
var myView = UIView(frame: CGRectMake(20, 0, lastCell!.frame.width, lastCell!.frame.height))
dailyCell.frame.size = myView.frame.size
}
您應該避免嘗試更改其他單元格的單元格。只需在一個單元格中執行操作即可更新數據模型,然後重新加載可能受影響的部分。假設你有你的代碼正確的單元格創建和返回高度,他們會照顧自己。所以過程應該是:單元格中的單元格或項目被單擊 - >更新數據模型 - >重新加載受影響的部分或行或整個表格。 –