我对ios移动应用程序开发和swift很陌生。我正在尝试实现uitableviewcontroller,它可以执行以下操作:
单击任何节标题时,应下拉对应于该特定节的单元格。
单击包含0个单元格的节标题时,应触发一个segue并将其推送到另一个viewcontroller。
0个单元格->推送到另一个viewcontroller。
n单元格->下拉列表
我怎样才能迅速做到这一点?
我在uitableviewcontroller中尝试了下面的代码来切换,但是没有成功。
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let frame: CGRect = tableView.frame
let button = UIButton(type: UIButtonType.System) as UIButton
button.frame = CGRectMake(5, 5, frame.size.width-10, 80)
button.addTarget(self, action: "menuButtonAction", forControlEvents: .TouchUpInside)
if section == 1 {
button.setTitle("Home", forState: UIControlState.Normal)
}else if section == 2 {
button.setTitle("Profile", forState: UIControlState.Normal)
}else if section == 3 {
button.setTitle("Projects", forState: UIControlState.Normal)
}else {
button.setTitle("Title", forState: UIControlState.Normal)
}
let headerView = UIView(frame: CGRectMake(0, 0, frame.size.width, 150))
headerView.addSubview(button)
return headerView;
}
var enabledCategory:Bool = false
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if (section == 3 && enabledCategory){
return dataHandler.getNumberOfProjects()
}
else {
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("menuCellIdentifier", forIndexPath: indexPath) as? TableViewCell
// Configure the cell...
if indexPath.section == 3 {
cell!.customLabel.text = String(indexPath.row)
}
return cell!
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 3 {
enabledCategory = !enabledCategory
tableView.reloadSections(NSIndexSet(index: 3) , withRowAnimation: .Fade)
}
}
而且,我不知道如何通过单击第1节和第2节来推送到另一个viewcontroller。
谢谢。
最佳答案
默认的uitableview头视图不可单击。您应该实现tableView(_:viewForHeaderInSection:)
的UITableViewDelegate
方法,并为自己的UIView
提供UIButton
或一些可点击的元素。