gpt4 book ai didi

ios - 如何将单元格中的 View 变为可见?

转载 作者:行者123 更新时间:2023-11-28 07:17:22 25 4
gpt4 key购买 nike

我有一个带有下拉自定义单元格的 tableView,我在这个单元格中有一些 View ,当单元格被选中时,它们必须变为可见,我尝试了一些实现,但没有任何效果。你能帮帮我吗?

我的自定义单元格类:

class daysWorkPointsCell: UITableViewCell {

@IBOutlet var dayOfMonth: UILabel
@IBOutlet var hourAndMinute: UILabel
@IBOutlet var greenBar: UIView
@IBOutlet var scroll: UIScrollView
@IBOutlet var hourView: UIView

// greenBar,scroll and hourView are invisible and I need to turn to visible when selected

init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}

我的 View Controller :

class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var daysWorkPointTable: UITableView

var selectedCellIndexPath: NSIndexPath?
let SelectedCellHeight: CGFloat = 150.0
let UnselectedCellHeight: CGFloat = 75.0

override func viewDidLoad() {
super.viewDidLoad()

var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil)

self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell")


}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 1
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){

if let selectedCellIndexPath = selectedCellIndexPath {

if selectedCellIndexPath == indexPath {
self.selectedCellIndexPath = nil
} else {
self.selectedCellIndexPath = indexPath
}

} else {
selectedCellIndexPath = indexPath
}

tableView.beginUpdates()
tableView.endUpdates()

}

func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat {

if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {

return SelectedCellHeight
}
}

return UnselectedCellHeight
}

// here I tried to change the visibility but don't work
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{

var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell

if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {

cell.greenBar.hidden = false
cell.scroll.hidden = false
cell.hourView.hidden = false
}else{
cell.greenBar.hidden = true
cell.scroll.hidden = true
cell.hourView.hidden = true
}
}

return cell
}

最佳答案

不需要 selectedIndexPath 在代码下面使用它会更干净

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{


for cell in tableView.visibleCells(){
var hidden:Bool = cell.greenBar.hidden
cell.greenBar.hidden = !hidden

hidden = cell.scroll.hidden
cell.scroll.hidden = !hidden

hidden = cell.hourView.hidden
cell.hourView.hidden = !hidden
}

var cell = tableView.cellForRowAtIndexPath(indexPath)

var hidden:Bool = cell.greenBar.hidden
cell.greenBar.hidden = !hidden

hidden = cell.scroll.hidden
cell.scroll.hidden = !hidden

hidden = cell.hourView.hidden
cell.hourView.hidden = !hidden


}

我在这里使用了 hidden 变量而不是 cell.greenBar.hidden = !cell.greenBar.hidden 因为在 swift 中 ! 也是用于解包,因此在与可选项一起使用时会产生一些问题

关于ios - 如何将单元格中的 View 变为可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25002269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com