我有一个带有下拉自定义单元格的tableView,我在这个单元格中有一些视图,当选择单元格时,它们必须转向可见,我尝试了一些实现,但没有任何工作。你能救我吗?
我的自定义单元格类:
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)
}
}
我的观点控制器:
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
}
}
答案 0 :(得分:1)
无需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 !
中也用于展开,所以当与选项一起使用时会产生一些问题