Ios 比较一个结构的两个值,并在UITableView swift 4.0中获取额外的单元格
我的代码包含一个tableview,我试图比较一个结构中的两个值,它给了我数组的总数,同时它甚至打印了比较过的数据,其中甚至包含不需要的额外单元格。我想比较cellID和cellID2的值,如果它们匹配,那么它应该只打印那些单元格!!!有人能帮我解决这个错误吗Ios 比较一个结构的两个值,并在UITableView swift 4.0中获取额外的单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我的代码包含一个tableview,我试图比较一个结构中的两个值,它给了我数组的总数,同时它甚至打印了比较过的数据,其中甚至包含不需要的额外单元格。我想比较cellID和cellID2的值,如果它们匹配,那么它应该只打印那些单元格!!!有人能帮我解决这个错误吗 首先,应该从数组中筛选出不希望在表视图中显示的元素。然后,将该过滤数组用于数据源。你不必把这种奇怪的情况写在你的手机里。。功能 class ViewController: UIViewController, UITableViewDele
首先,应该从数组中筛选出不希望在表视图中显示的元素。然后,将该过滤数组用于数据源。你不必把这种奇怪的情况写在你的手机里。。功能
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
}
}
你的方法不对。首先,应该从数组中筛选出不希望在表视图中显示的元素。然后,将该过滤数组用于数据源。如果你遵循这一点,你就不必在你的cellForRowAt中写下奇怪的条件:。。函数。@nayem你能帮我过滤数组吗?我想比较cellID和cellID2的值,如果它们匹配,那么它应该只打印那些单元格!!!看到我的答案了吗below@nayem是的,我正在尝试,我有4个值匹配我的数组,但它甚至打印了一个不匹配的好。不要像这样编辑答案。它应该是一个评论。您错过了tableView:cellForRowAt:中的部分。你必须在这里使用filteredArray。好的,我使用了它,它工作得很成功,但是如果我想将数组值与变量进行比较,我该怎么做呢???如果让userID=companyDefaultValue.stringforKey:user\u type\u id{printuser type id=userID}我想将这个值与我的一个struct valueWell进行比较,这是另一个问题。你应该寻找类似的东西,或者单独问一个新问题。
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
}
}