gpt4 book ai didi

swift - 在 TableView 部分中选择单元格

转载 作者:行者123 更新时间:2023-11-28 09:36:44 33 4
gpt4 key购买 nike

我一直在寻找有关如何执行此操作的答案,但似乎没有什么是直截了当的,或者用户想要做一些不同的事情(例如选择多个单元格)。

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

“设置”选项卡是一个包含 3 个部分的 UITableView,每个部分有 2 个单元格。

问题:我想让每个单元格去不同的目的地。第一部分中的第一个单元格只是一个带有 slider 的 View Controller (用于更改文本颜色的颜色 slider )(稍后会介绍)。我怎样才能做到这一点?

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。如果您需要更多信息,只需评论您想要添加的内容,我会立即编辑这篇文章。提前致谢!

最佳答案

您可以使用 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")
}
}

尝试选择每一行以显示它打印的部分和打印的行。

关于swift - 在 TableView 部分中选择单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558333/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com