gpt4 book ai didi

ios - 在 UITableView 中选择行时不会更新变量

转载 作者:行者123 更新时间:2023-11-28 12:18:47 24 4
gpt4 key购买 nike

我试图用在 UITableView 中选择的行号修改一个变量,这样我就可以从另一个 UIViewController 访问该变量,但是当我用 中的行号设置变量的值时>func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 变量似乎没有改变。

我有一个带有 UITableView 的 View Controller ,我想要的是选择一行,然后,当我单击一个按钮时,会出现一个 Popoverview,我可以在其中参数化链接到我选择的行的内容。

这是我所做的:

import UIKit

class Settings: UIViewController , UITableViewDataSource,
UITableViewDelegate{

@IBOutlet weak var tableView: UITableView!
var RowSelected = Int()

let animals = ["Tap", "Double Tap", "Long press", "Swipe up", "Swipe down", "Swipe left", "Swipe right", "Zoom", "Unzoom"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"/*Identifier*/, for: indexPath as IndexPath)
cell.textLabel?.text = animals[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animals.count
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

RowSelected = indexPath.row
print(RowSelected)
}

}

它在这里完美地打印了行,但是当我从另一个 ViewController 访问它时它总是等于 0。

import UIKit

class GestureConfiguration: UIViewController,
UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var actionSelected: UILabel!

let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5"]
var index : Int = 1

var scVC = Settings()

let gestes = ["Tap", "Double Tap", "Long press", "Swipe up", "Swipe down", "Swipe left", "Swipe right", "Zoom", "Unzoom"]

@IBOutlet weak var tableView: UITableView!

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 9
}

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return self.items.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


collectionView.allowsMultipleSelection = true

// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 1

return cell
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
let cell = collectionView.cellForItem(at: indexPath)
print("You selected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
cell?.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1.0)
///////// HERE \\\\\\\\\\
actionSelected.text = String(scVC.RowSelected)
print(scVC.RowSelected)
// Always print 0, same for the label.
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
// handle tap events
let cell = collectionView.cellForItem(at: indexPath)
print("You unselected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
cell?.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
}

我错过了什么?当我将一个值硬编码到 RowSelected(如 99)时,我能够在我的第二个 ViewController 中看到 99。

感谢您的帮助。

Akhilrajtr 的编辑:

class Settings: UIViewController , UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var tableView: UITableView!
var RowSelected = Int()

let animals = ["Tap", "Double Tap", "Long press", "Swipe up", "Swipe down", "Swipe left", "Swipe right", "Zoom", "Unzoom"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell"/*Identifier*/, for: indexPath as IndexPath)
cell.textLabel?.text = animals[indexPath.row]
return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animals.count
}

//Not overriding any function,
Override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "toPopover") {
var secondViewContr = segue.destination as! GestureConfiguration
secondViewContr.scVC = self
}
}
}

并且:

class GestureConfiguration: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var actionSelected: UILabel!

var scVC = Settings()

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
let cell = collectionView.cellForItem(at: indexPath)
print("You selected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
cell?.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1.0)

actionSelected.text = String(scVC.RowSelected)
print(scVC.RowSelected)
}

最佳答案

GestureConfiguration 中创建一个 Settings class 变量,然后在 tableViewDelegate 函数中访问它。

Settings 类中覆盖函数 prepareforsegue as

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

if let gestuedistination = segue.destination as? GestureConfiguration {
gestuedistination.settings = self
}
}

GestureConfiguration类中声明

@IBOutlet weak var actionSelected: UILabel!
var settings:Settings?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap events
let cell = collectionView.cellForItem(at: indexPath)
print("You selected cell #\(indexPath.item + 1) section \(indexPath.section + 1)")
cell?.backgroundColor = UIColor(red:0.08, green:0.28, blue:0.45, alpha:1.0)
actionSelected.text = String(settings!.RowSelected)
print(settings!.RowSelected)
}

关于ios - 在 UITableView 中选择行时不会更新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45506235/

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