繁体   English   中英

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

[英]How to get data from the multiple selected rows and show in another VC tableview cell Swift4

您好,我试图从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")
        }

    }

}

就像@Rakshith在他们的评论中建议的那样,将数据(labone,labtwo,img数组)移出视图控制器,然后移入模型对象。 将该模型对象传递给第二个视图控制器。

还要在第二个视图控制器中创建一个数组selectedIndexPaths 当您点击ViewController的按钮时,获取选定索引路径的数组并将其传递给第二个视图控制器。

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

您应该使用如下所示的某种结构,然后更新所选或取消选中的项目的状态,然后当您要转到下一个viewController时,可以使用以下所选的项目过滤dataSource,

 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM