gpt4 book ai didi

ios - 在 swift iOS 中从表视​​图中选择时,UIsearchbar 结果出错

转载 作者:行者123 更新时间:2023-11-28 14:37:32 25 4
gpt4 key购买 nike

在我当前的应用程序中,我显示了学生列表。我给了一个复选标记按钮供选择。我可以从表格中选择学生,但每当我尝试从搜索栏结果中选择时,它都无法正常工作。

例如,在列表中,我选择了索引为 0 和 1 的对象;每当我尝试搜索某些内容时,都会自动选择索引 0 和 1 处的搜索栏结果对象。为什么会这样,我该如何解决?

// MARK:- seachbar methods
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
search_bar.showsCancelButton = true

print("SERACH TEXT ",searchText)
if searchText.isEmpty{
is_searching = false
} else {
print(" search text %@ ",searchText as NSString)
is_searching = true
searchingDataArray.removeAllObjects()
for index in 0 ..< studentsArray.count
{
var localDic :NSDictionary!
localDic = studentsArray.object(at: index) as! NSDictionary
let nameString = localDic["name"] as? NSString
let regString = localDic["reg_no"] as? NSString

if (nameString?.localizedCaseInsensitiveContains(searchText))! || (regString?.localizedCaseInsensitiveContains(searchText))!
{
searchingDataArray.add(localDic)
} else {
print("Not Exists")
}
print(" search text %@ ",searchingDataArray)
self.tutorListView.reloadData()
}
}
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
{
is_searching = false
search_bar.text = ""
search_bar.showsCancelButton = false
self.tutorListView.reloadData()
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
is_searching = false
self.tutorListView.reloadData()
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SKTutorCell", for: indexPath) as! SKTutorCell

var localDic :NSDictionary!

if is_searching
{
localDic = searchingDataArray.object(at: indexPath.row) as! NSDictionary
}
else
{
localDic = studentsArray.object(at: indexPath.row) as! NSDictionary
}

cell.nameRegdNoLbl.text = localDic?.value(forKey: "name") as? String

cell.checkMarkBtn.tag = indexPath.row
cell.checkMarkBtn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)

if ((studentcheckArray[indexPath.row] as AnyObject).isEqual("0")) {
cell.checkMarkBtn.setImage(UIImage(named:"UnCheckIcon"), for: .normal)
}else{
cell.checkMarkBtn.setImage(UIImage(named:"CheckIcon"), for: .normal)
}

cell.seeProfileBtn.tag = indexPath.row
cell.seeProfileBtn.addTarget(self, action: #selector(seeProfilAction(_:)), for: .touchUpInside)
return cell
}

@objc func checkBoxSelection(_ sender:UIButton)
{
let btnTag = sender.tag
print("buttonRow--",btnTag)
if ((studentcheckArray[btnTag] as AnyObject).isEqual("0")) {
studentcheckArray[btnTag] = "1"
}
else {
studentcheckArray[btnTag] = "0"

}
var NewArray = NSMutableArray()
for i in 0..<studentcheckArray.count {
if ((studentcheckArray[i] as AnyObject).isEqual("1")) {
NewArray.add(studentcheckArray[i])
print("NewArray",NewArray)
}
}
if NewArray.count < studentcheckArray.count {
selectAllBtn.setImage(#imageLiteral(resourceName: "UnCheckIcon"), for: .normal)
}
self.tutorListView.reloadData()
}

最佳答案

这样试试

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SKTutorCell", for: indexPath) as! SKTutorCell

var localDic :NSDictionary!

if is_searching
{
localDic = searchingDataArray.object(at: indexPath.row) as! NSDictionary

if ((searchingDataArray[indexPath.row] as AnyObject).isEqual("0")) {

cell.checkMarkBtn.setImage(UIImage(named:"UnCheckIcon"), for: .normal)
}else{
cell.checkMarkBtn.setImage(UIImage(named:"CheckIcon"), for: .normal)
}

}
else
{
localDic = studentsArray.object(at: indexPath.row) as! NSDictionary


if ((studentcheckArray[indexPath.row] as AnyObject).isEqual("0")) {
cell.checkMarkBtn.setImage(UIImage(named:"UnCheckIcon"), for: .normal)
}else{
cell.checkMarkBtn.setImage(UIImage(named:"CheckIcon"), for: .normal)
}
}


cell.nameRegdNoLbl.text = localDic?.value(forKey: "name") as? String

cell.checkMarkBtn.tag = indexPath.row
cell.checkMarkBtn.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)


cell.seeProfileBtn.tag = indexPath.row
cell.seeProfileBtn.addTarget(self, action: #selector(seeProfilAction(_:)), for: .touchUpInside)
return cell
}

关于ios - 在 swift iOS 中从表视​​图中选择时,UIsearchbar 结果出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com