我有一個tableView單元格由UIStackViews填充,按鈕作爲arrangeSubviews,這是用幾個函數創建的。 stackView中的頂視圖是可見的,所有其他視圖都是隱藏的,頂視圖中的按鈕有一個動作,當其它視圖調用時,它應該在可見和隱藏之間切換。如何在UIStackView中切換arrangeSubviews的可見性,那是在一個tableViewCell swift3
func generateButtons() -> [[UIButton]]{
var topButtonArray = [UIButton]()
var finalButtonArray = [[UIButton]]()
for title in array1 {
topButtonArray.append(createButton(title: title , action: "buttonPressed"))
}
for button in topButtonArray {
var buttonArray = [UIButton]()
buttonArray.append(button)
for title in array2 {
buttonArray.append(createButton(title: title, action: "moveOn"))
}
finalButtonArray.append(buttonArray)
}
return finalButtonArray
}
func generateStackViews() -> [UIStackView] {
stackViewArray = [UIStackView]()
let finalButtonArray = generateButtons()
for buttons in finalButtonArray{
stackViewArray.append(createStackView(subViews: buttons))
}
for stackView in stackViewArray{
let views = stackView.arrangedSubviews
let hiddenViews = views[1..<views.count]
for views in hiddenViews{
views.isHidden = true
}
}
return stackViewArray
}
func buttonPressed(){
//let stackViewArray = generateStackViews()
for stackView in stackViewArray{
let views = stackView.arrangedSubviews
let hiddenViews = views[1..<views.count]
for view in hiddenViews {
if view.isHidden == true{showViews(view: view)} else{hideViews(view: view)}
}
}
}
func showViews(view : UIView){
UIView.animate(withDuration: 0.3) {
view.isHidden = false
}
}
func hideViews(view : UIView) {
UIView.animate(withDuration: 0.2) {
view.isHidden = true
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "First")!
let stackViewArray = generateStackViews()
cell.contentView.addSubview(stackViewArray[indexPath.row])
return cell
}
現在什麼情況是,只在最後一個單元格中隱藏的觀點是可見的,隱藏的(無論我點擊哪個單元)之間進行切換 - 我想我需要實例上的所有細胞中來回切換,但我不能找出一個方法來做到這一點。
另一個問題是,我想要一個頂視圖只在其單元格中打開隱藏的視圖,我想我需要在'cellForRowAt indexPath'以外以某種方式使用indexPath.row。