2016-06-01 143 views
0

我一直在寻找一个关于如何做到这一点的答案,但没有一件事看起来很简单,或者用户想要做一些不同的事情(比如选择多个单元格)。在TableView中选择单元格

背景: 我正在制作一个应用程序,每天为不同的职业(如阅读,园艺,运动)检索报价。我有一个带有3个选项卡的UITabBarController。
First tab = Quote;第二个标签=分类;第三个选项卡=设置*

设置选项卡是一个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。如果您需要更多信息,只需评论您希望添加的内容,然后立即编辑此帖子。先谢谢你!

回答

3

该部分中的部分和项目数量是否会发生变化?如果没有,创建静态单元格并将每个单元格连接到不同的目标可以使用无代码完成(全部在Interface Builder中)。

+0

谢谢。工作。当它说静态单元格只适用于UIViewControllers时,我遇到了一个问题,但我解决了它。 –

3

您可以使用indexPath.sectionindexPath.row通过您的最后一个功能didSelectRowAtIndexPath访问每个部分和每一行。例如,如果你要访问的第二部分和第一排,你可以做

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    print("Section \(indexPath.section), Row : \(indexPath.row)") 
    if indexPath.section == 1 && indexPath.row == 0{ 
     //Code to implement View Controller to remove adds 
     print("Remove Adds") 
    } 
} 

尝试选择的每一行,以显示它打印和哪一行哪一节。

+0

谢谢你的帮助。 –

相关问题