我一直在寻找一个如何做到这一点的答案,但似乎没有什么是直接的,或用户想做一些不同的事情(如选择多个单元格)。
背景:
我正在制作一个应用程序,每天检索不同职业的报价,如阅读,园艺,体育。我有一个带三个标签的UITabBarController。
第一个选项卡=报价;第二个选项卡=类别;第三个选项卡=设置*
“设置”选项卡是一个UITableView,其中有3个节,每个节中有2个单元格。
问题:我想让每个细胞去不同的目的地。第一部分中的第一个单元格将只是一个带有滑块的视图控制器(颜色滑块用于更改文本颜色)(稍后将介绍)。我怎样才能做到这一点?
import UIKit
class settingsTab: UIViewController, UITableViewDelegate {
struct Objects {
var sectionName : String!
var sectionObjects : [String]!
}
var objectsArray = [Objects]()
override func viewDidLoad() {
super.viewDidLoad()
objectsArray = [Objects(sectionName: "Options", sectionObjects: ["Change color of text" , "Notifications"]),Objects(sectionName: "Extras", sectionObjects: ["Remove ads" , "Restore purchases"]),Objects(sectionName: "Support", sectionObjects: ["Rate this app" , "Email developer"]), Objects(sectionName: "Jordan Norris - Quote Daily 2016", sectionObjects: [])]
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
cell.backgroundColor = UIColor.cyanColor()
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectsArray.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return objectsArray[section].sectionName
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
这是settingsTab.swift,在这里建立TableViewController。如果你需要更多的信息,只需评论你想添加什么,我会立即编辑这篇文章。提前谢谢你!
最佳答案
节中节和项的数目是否会更改?如果没有,创建静态单元格并将一个段连接到每个单元格到不同的目的地就可以不用代码(都在Interface Builder中)。
关于swift - 在TableView部分中选择单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37558333/