Q:

compare two values of one struct and getting extra cells in UITableView swift 4.0

My code which contains a tableview where I am trying to compare two values from one struct it's giving me total number of array while its even printing the compared data which contains even extra cell that is not required. I want to compare cellID and cellID2 values if they match then it should print only those cells !!! Can someone help me with this error
import UIKit

struct One {
    let cellID: String
    let name: String
    let lastName: String
    let cellID2: String
}

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var arrayOne = [One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1")]

    @IBOutlet weak var compareTableView: UITableView!

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CompareTableViewCell") as! CompareTableViewCell
        let arrayID = arrayOne[indexPath.row]
        if arrayID.cellID == arrayID.cellID2{
            cell.lblOne.text = arrayID.cellID
            cell.lblTwo.text = arrayID.cellID2
            cell.lblThree.text = arrayID.lastName
            cell.lblFour.text = arrayID.name
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
}

A:

First of all, you should filter out the elements from the array that you don't want to show in the table view. Then you will use that filtered array for your data source. You won't have to write the weird condition in your cellForRowAt:.. function.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    let arrayOne = [One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "2"), One(cellID: "1", name: "hello", lastName: "hello last", cellID2: "1")]

    var filteredArray = [One]()

    @IBOutlet weak var compareTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Here you apply your filter
        filteredArray = arrayOne.filter { $0.cellID == $0.cellID2 }
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CompareTableViewCell") as! CompareTableViewCell
        let arrayID = filteredArray[indexPath.row]
        // Feed your cell
        return cell
    }
}

