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 errorMy Simulator screenshot

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
    }
}
Audrey Rozario
  • 59
  • 1
  • 10

1 Answers1

1

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
    }
}
nayem
  • 7,285
  • 1
  • 33
  • 51
  • I have 4 values that match my array but its even printing the one that does not match – Audrey Rozario May 21 '18 at 06:27
  • Okay. Don't edit answers like this. It should be a comment instead. You missed the part inside the `tableView(:cellForRowAt:)`. You have to use the `filteredArray` here. – nayem May 21 '18 at 06:27
  • ok i used and its working successfully but if i want to compare an array value with a variable how am i supposed to do that???? – Audrey Rozario May 21 '18 at 06:59
  • if let userID = companyDefaultValue.string(forKey: "user_type_id"){ print("user type ID=\(userID)") } I want to compare this value with my One of my struct value – Audrey Rozario May 21 '18 at 07:00
  • Well, that is another question. You should look for something similar or ask a new question separately. – nayem May 21 '18 at 07:05
  • I tried looking out for an answer but I did'nt get any yet can you help me with this??? – Audrey Rozario May 21 '18 at 07:09
  • Have you asked the question? If not, then how should I know what's your purpose and what have you tried? – nayem May 21 '18 at 07:11
  • I am trying to compare a variable value with a struct value and if it matches it should print the values that match – Audrey Rozario May 21 '18 at 07:13
  • I don't get it what you are looking for. You want to filter your array (of structs) by matching with another variable, right? The word **compare** is ambiguous and not within the context. It's best to ask another question, you describe your need with sample input and output. – nayem May 21 '18 at 07:19
  • ok I'll ask another question Thanks though!!! I hope you'd help me with my next question – Audrey Rozario May 21 '18 at 07:21
  • I have posted another question can you help me with that – Audrey Rozario May 21 '18 at 07:35
  • hello can you help me with my new question https://stackoverflow.com/questions/50501640/compare-value-from-two-struct-in-swift – Audrey Rozario May 24 '18 at 05:32