代码之家  ›  专栏  ›  技术社区  ›  Shahzad ali

如何从多个选定行中获取数据并在另一个VC tableview单元格Swift4中显示

  •  1
  • Shahzad ali  · 技术社区  · 6 年前

    您好,我从2天开始尝试完成我的任务,但到处都没有搜索成功,我想从tableview单元格(包含2个标签和一个图像)中选择多行,然后我想转移到另一个vc并在tableview中显示,我可以选择多行并从所选行中获取此类型索引,但现在我不知道如何获取数据并将其传输到另一个vc并在表视图中显示。请帮助我获取所选行索引,如下所示[[0,1],[0,2],[0,3]

    VC代码

    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
        @IBOutlet weak var tableview: UITableView!
    
    
        var labone = ["1","2","3","4","5","6"]
        var labtwo = ["a","b","c","d","e","f"]
        var img =  ["bag","bag","bag","bag","bag","bag"]
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        @IBAction func button(_ sender: Any) {
    
            let selectedindexPath = tableview.indexPathsForSelectedRows
            if(selectedindexPath != nil){
                let toy  = labone[0]
                print(toy)
                print(selectedindexPath) }
    
            else{
                print("null")
            }
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
            return labone.count
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    
             let name = cell.viewWithTag(1) as! UILabel
            let name_two = cell.viewWithTag(2) as! UILabel
            let imgg = cell.viewWithTag(3) as! UIImageView
    
            name.text = labone[indexPath.row]
            name_two.text = labtwo[indexPath.row]
            imgg.image = UIImage(named: img[indexPath.row])
    
    
            return cell
    
        }
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
            cell.contentView.backgroundColor = UIColor.yellow
    
        }
    
    
        func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            // let selectedCell = tableview.cellForRow(at: indexPath)
            if let label = cell?.contentView.viewWithTag(4) as? UIImageView {
                label.image = UIImage(named: "check_disable")
            }
    
        }
    
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Kamran    6 年前

    您应该使用下面这样的结构,然后更新选中或取消选中项目的状态,然后当您想转到下一个viewController时,可以使用下面所选的项目过滤数据源,

     class CellModel {
    
        var labelOne: String
        var labelTwo: String
        var imageName: String
        var isSelected = false
    
        init(_ labelOne: String, labelTwo: String, imageName: String) {
            self.labelOne = labelOne
            self.labelTwo = labelTwo
            self.imageName = imageName
        }
    }
    

    更新viewController,

    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
       let dataSource = [CellModel("1", labelTwo: "a", imageName: "bag"),
                         CellModel("2", labelTwo: "b", imageName: "bag"),
                         CellModel("3", labelTwo: "c", imageName: "bag"),
                         CellModel("4", labelTwo: "d", imageName: "bag"),
                         CellModel("5", labelTwo: "e", imageName: "bag"),
                         CellModel("6", labelTwo: "f", imageName: "bag")]
    }
    

    然后你可以更新你的cellForRowAt,如下所示,

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
             let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    
           let name = cell.viewWithTag(1) as! UILabel
           let name_two = cell.viewWithTag(2) as! UILabel
           let imgg = cell.viewWithTag(3) as! UIImageView
    
           let model = self.dataSource[indexPath.row]
           name.text = model.labelOne
           name_two.text = model.labelTwo
           imgg.image = UIImage(named: model.imageName)
    
           return cell
        }
    

    使用以下命令更新numberOfRowInSection

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

    当选择/删除单元时,您可以更新模型的状态,如下所示,

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
            cell.contentView.backgroundColor = UIColor.yellow
            self.dataSource[indexPath.row].isSelected = true
        }
    
    
        func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
            // let selectedCell = tableview.cellForRow(at: indexPath)
            if let label = cell?.contentView.viewWithTag(4) as? UIImageView {
                label.image = UIImage(named: "check_disable")
                self.dataSource[indexPath.row].isSelected = false
          }
     }
    

    最后,在按钮操作中,您可以过滤选定的模型并传递到下一个viewController

    @IBAction func button(_ sender: Any) {
    
         let selectedItems = self.dataSource.filter({ $0.isSelected == true })
         // Pass to next ViewController the selectedItmes
    }
    
        2
  •  0
  •   Duncan C    6 年前

    正如@Rakshith在他们的评论中所建议的那样,将数据(labone、labtwo、img数组)移出视图控制器并移动到模型对象中。将该模型对象传递给第二个视图控制器。

    同时创建阵列 selectedIndexPaths 在第二个视图控制器中。当您在 ViewController ,获取所选索引路径的数组,并将其传递给第二个视图控制器。

    在第二视图控制器中 viewWillAppear ,使用 选定的索引路径 变量将项目所选项目复制到数组itemsToDisplay中,并在表视图数据源方法中使用该数组填充第二个视图控制器的表视图。