簡體   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