Search code examples
iosswiftuitableviewtableviewuisegmentedcontrol

how to load selected data from a table view when a segment is selected?


I wanted to know how to load selected data from my table view, for example when selecting segment index is equal 1, the table view will reload and will only show data which status is equal to approved. Cause as you have seen from my below code, I have loaded all the data with all the statuses: . for example if segmentView.selectedSegmentIndex == 1 table will reload with the data which status is equal to approved. I could already determine the selected index. what i want is how to access those data from the table view that i could load selected data depending to status

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // Table view cells are reused and should be dequeued using a cell identifier.
        let cellIdentifier = "ToDoListTableViewCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ToDoListTableViewCell
        cell.delegate = self


//        let toDoActionItem = fetchedResultsController.object(at: indexPath)

        if let getTempDetails: [String : Any] = getAllDetail[indexPath.row] {
            print("ang tanan data:" , getTempDetails)
            if let str = getTempDetails["status"] as? [String: String] {
                if let name = str["name"] {
                    if name == "ongoing" {
                        cell.toDoItemLabel.text = getTempDetails["name"] as? String
                        cell.statuslabel.backgroundColor = created
//                        cell.label.textColor =  UIColor(red: 0.9294, green: 0.3333, blue: 0.1804, alpha: 1.0)
//                        cell.backgroundColor = created


                    }
                    else if name == "approved" {
                       cell.toDoItemLabel.text = getTempDetails["name"] as? String
                       cell.statuslabel.backgroundColor = done
                       cell.checkBoxButton.isSelected =  true
                    }
                    else if name == "for approval" {
                        cell.toDoItemLabel.text = getTempDetails["name"] as? String
                        cell.statuslabel.backgroundColor = pending
                    }else if name == "near expiry" {
                       cell.toDoItemLabel.text = getTempDetails["name"] as? String
                        cell.statuslabel.backgroundColor = neardue
                    } else if name == "expired" {
                        cell.toDoItemLabel.text = getTempDetails["name"] as? String
                        cell.statuslabel.backgroundColor = expired
                    } else {
                        print("false")
                       cell.toDoItemLabel.text = "LOLS"
                    }
                }
                }
            }



    code for segment (in selecting segment)

    func selectSegmentInSegmentView(segmentView: SMSegmentView) {

        if segmentView.selectedSegmentIndex == 1 {
            print("ang index nga emo ge click is one")
            //            let selectedSegment : SMSegment = segmentView.selectedSegment!
            //            self.userName = selectedSegment.label.text!

        } else {

            logic here
        }
        self.setUpTableView()
or could be self.tableView.reloadData()
    }

Solution

  • You just need to create 5 separate arrays to load while selection of different segment of UISegmentControl.

    var ongoingArr = [[String: Any]]() // For segment index 0
    var approvedArr = [[String: Any]]() // For segment index 1
    var forApprovalArr = [[String: Any]]() // For segment index 2
    var nearExpiryArr = [[String: Any]]() // For segment index 3
    var expiredArr = [[String: Any]]() // For segment index 4
    

    You have the whole data getAllDetail, and you are getting the data in this array by API or previous screen:

    var getAllDetail = [[String: Any]]()
    

    If you are getting the data from API, then load above 5 arrays after loading the data in getAllDetail. Fo that just create an extension of array as:

    extension Array where Element == [String: Any] {
    
        func filterArray(_ statusName: String) -> [Element] {
            return self.filter { infoDict -> Bool in
                if let statusDict = infoDict["status"] as? [String: String], let name = statusDict["name"] {
                    return name == statusName
                }
                return false
            }
        }
    
    }
    

    and, load above 5 arrays:

    func loadSegmentArray() {
    
        ongoingArr = getAllDetail.filterArray("ongoing")
        approvedArr = getAllDetail.filterArray("approved")
        forApprovalArr = getAllDetail.filterArray("for approval")
        nearExpiryArr = getAllDetail.filterArray("near expiry")
        expiredArr = getAllDetail.filterArray("expired")
    
        // Select 0th index of segment and reload table
        segmentView.selectedSegmentIndex = 0
        self.setUpTableView() // Reload Table view
    }
    

    In your func selectSegmentInSegmentView, just reload table view:

    func selectSegmentInSegmentView(segmentView: SMSegmentView) {
        self.setUpTableView()
    }
    

    And update your UITabelView delegate and datasource methods, according to these 5 arrays.

    For Sample, I am writing numberOfRowsInSection and cellForRowAt indexPath:

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch segmentView.selectedSegmentIndex {
        case 0:
            return ongoingArr.count
        case 1:
            return approvedArr.count
        case 2:
            return forApprovalArr.count
        case 3:
            return nearExpiryArr.count
        case 4:
            return expiredArr.count
        default:
            return 0
        }
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "ToDoListTableViewCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ToDoListTableViewCell
        cell.delegate = self
    
        var infoDict = [String: Any]()
        switch segmentView.selectedSegmentIndex {
        case 0:
            infoDict = ongoingArr[indexPath.row]
            cell.statuslabel.backgroundColor = // Color of onging
        case 1:
            infoDict = approvedArr[indexPath.row]
            cell.statuslabel.backgroundColor = // Color of approvedArr
        case 2:
            infoDict = forApprovalArr[indexPath.row]
            cell.statuslabel.backgroundColor = // Color of forApprovalArr
        case 3:
            infoDict = nearExpiryArr[indexPath.row]
            cell.statuslabel.backgroundColor = // Color of nearExpiryArr
        case 4:
            infoDict = expiredArr[indexPath.row]
            cell.statuslabel.backgroundColor = // Color of expiredArr
        default:
            cell.statuslabel.backgroundColor = .black
        }
    
        cell.toDoItemLabel.text = infoDict["name"] as? String
        return cell
    }