Home > Software design >  When I tapped segment Odd or even I am getting true and false value in Tableview instead of odd valu
When I tapped segment Odd or even I am getting true and false value in Tableview instead of odd valu

Time:09-25

When I press odd it shows true or false. How can Get values of array I'm trying to print odd value when i tap odd on segment or even values when i tap even , How can get this. I'm new in ios. Please help

class ViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var segmentOutlet: UISegmentedControl!
    let numberArray = Array(1...100)
    var segmentIndex  = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.delegate = self
        tableView.dataSource = self
    }
    @IBAction func segmentControl(_ sender: UISegmentedControl) {
        

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
        cell.numberLbl.text = String(numberArray[indexPath.row])
        if segmentOutlet.selectedSegmentIndex == 1 {
            cell.numberLbl.text = String(numberArray[indexPath.row] % 2 == 0)
            
        }
        if segmentOutlet.selectedSegmentIndex == 2 {
            cell.numberLbl.text = String(numberArray[indexPath.row] % 2 == 1)
        }
        
        return cell
    }
}

my Simulator image:

code file

CodePudding user response:

Your numberArray[indexPath.row] % 2 == 0 has a ==, which evaluates to true/false. It doesn't really make sense. Also, there's no need to check if the current indexPath.row is odd or even... you're trying to display a list of even or odd numbers, so what does indexPath.row have to do with that?

Instead, try making a second array, filteredArray. This will be where the table view directly gets its data from.

let numberArray = Array(1...100)
lazy var filteredArray: [Int] = { numberArray }() /// return `numberArray` by default

You can then update filteredArray based on the selectedSegmentIndex.

@IBAction func segmentControl(_ sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 1: /// even numbers
        filteredArray = numberArray.filter { $0 % 2 == 0}
    case 2: /// odd numbers
        filteredArray = numberArray.filter { $0 % 2 == 1}
    default: /// all numbers
        filteredArray = numberArray
    }
    tableView.reloadData()
}

Finally, just read from filteredArray in your data source methods.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filteredArray.count /// use `filteredArray`
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
    cell.numberLbl.text = String(filteredArray[indexPath.row]) /// also use `filteredArray`
    return cell
}
  • Related