【发布时间】:2016-04-27 17:21:26
【问题描述】:
我是 iOS 移动应用程序开发和 Swift 的新手。我正在尝试实现一个 UITableViewController 可以执行以下操作:
- 单击任何部分标题时,对应于该特定部分的单元格应下拉。
- 单击包含 0 个单元格的节标题后,应触发 segue 并将其推送到另一个 ViewController。
0 个单元格 -> 推送到不同的 ViewController。
n 个单元格 -> 下拉列表
如何在 Swift 中实现这一点?
我在 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。
谢谢。
【问题讨论】:
-
没有。您应该提供您尝试过的内容,然后我们可以帮助改进。没有人会为您编写代码。
标签: swift uitableview mobile-application