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

霍基多基

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

我有一个带有 UITableView 的视图控制器,我想要的是选择一行,然后,当我单击一个按钮时,会出现一个 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

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)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在UITableView IOS中选择行时多个复选标记

来自分类Dev

在UITableView IOS中选择行时多个复选标记

来自分类Dev

UITableView不更新

来自分类Dev

searchDisplayController不更新UITableVIew

来自分类Dev

在uitableview部分中选择行

来自分类Dev

如何限制在uitableview中选择的行数

来自分类Dev

如何从 UITableView 的多个部分中选择多行?

来自分类Dev

UITableView滚动不滚动

来自分类Dev

UITableView刷新而不滚动

来自分类Dev

UITableView didSelectRowAt不注册

来自分类Dev

UITableView与SKSTableViewCell选择问题

来自分类Dev

UITableView导航行选择

来自分类Dev

UITableview有多个选择?

来自分类Dev

处理UITableView选择堆栈

来自分类Dev

每秒更新UITableView

来自分类Dev

每秒更新UITableView

来自分类Dev

UITableView 更新失败

来自分类Dev

如何异步更新 UITableView?

来自分类Dev

UITableView 的数据源不更新数组

来自分类Dev

在UITableView(swift2)中选择单元格时更新/重新加载ViewController

来自分类Dev

从UITableView中选择行时,将Core Data对象传递给另一个视图控制器

来自分类Dev

在UICollectionView选择上选择UITableView

来自分类Dev

在UITableView中选择所有行的更简便方法

来自分类Dev

在UITableView iOS的每个部分中选择一行?

来自分类Dev

如何在编辑模式下的UITableView中选择多行?

来自分类Dev

在UITableView iOS的每个部分中选择一行?

来自分类Dev

无法在UITableView中选择正确的第一行

来自分类Dev

在Swift的cellForRowAtIndexPath中的UITableView中选择检测单元格

来自分类Dev

UITableView 无意中选择了多个单元格