代码之家  ›  专栏  ›  技术社区  ›  Audrey Rozario

比较一个结构的两个值并在uiTableView Swift 4.0中获取额外单元格

  •  0
  • Audrey Rozario  · 技术社区  · 7 年前

    我的代码包含一个TableView,我试图比较一个结构中的两个值,它给了我数组的总数,同时它甚至打印了包含甚至不需要的额外单元格的比较数据。我想比较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 回复  |  直到 7 年前
        1
  •  1
  •   nayem    7 年前

    首先,您应该从不希望在表视图中显示的数组中筛选出元素。然后,您将对数据源使用筛选后的数组。你不必写在你的 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
        }
    }