сравнить два значения одной структуры и получить дополнительные ячейки в UITableView swift 4.0 - PullRequest
0 голосов
/ 21 мая 2018

Мой код, который содержит табличное представление, в котором я пытаюсь сравнить два значения из одной структуры, дает мне общее количество массивов и даже печатает сравниваемые данные, которые содержат даже лишнюю ячейку, которая не требуется.Я хочу сравнить значения cellID и cellID2, если они совпадают, тогда он должен печатать только эти ячейки !!!Может кто-нибудь помочь мне с этой ошибкой My 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
    }
}

1 Ответ

0 голосов
/ 21 мая 2018

Прежде всего, вы должны отфильтровать элементы из массива, которые вы не хотите показывать в табличном представлении.Затем вы будете использовать этот фильтрованный массив для вашего источника данных.Вам не нужно будет писать странные условия в вашей функции cellForRowAt:...

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
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...