大佬教程收集整理的这篇文章主要介绍了iOS Swift – 过滤具有搜索栏和搜索显示的自定义单元格的UITableView内容,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的ViewController:
DomandeViewController.swift
import UIKit class DomandeViewController: UIViewController,UITableViewDatasource,UITableViewDelegate,UISearchDisplayDelegate,UISearchBarDelegate { var arrayDomande = [Domanda]() var areaDomande: Area = Area() var argomentoDomande: Argomento = Argomento() var domandeFiltrate = [Domanda]() @IBOutlet weak var questionTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. arrayDomande = ModelManager.instance.getQuestionsWithArguments(inAreaId: areaDomande.id,aboutId: argomentoDomande.id) self.searchDisplayController!.searchResultsTableView.registerClass(QuestionCellTableViewCell.classForCoder(),forCellReusEIDentifier: "QuestionCell") } func numberOfSectionsInTableView(tableView: UITableView) -> Int { if(tableView == self.searchDisplayController!.searchResultsTableView){ return 1 }else{ return 1 } } func tableView(tableView: UITableView,numberOfRowsInSection section: int) -> Int { if tableView == self.searchDisplayController!.searchResultsTableView { return domandeFiltrate.count } else { return arrayDomande.count } } func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell var domanda = Domanda() if tableView == searchDisplayController!.searchResultsTableView { domanda = domandeFiltrate[indexPath.row] } else { domanda = arrayDomande[indexPath.row] } cell.testoLabel.text = domanda.testo cell.numeroLabel.text = String(domanda.numero) return cell } func filterContentForSearchText(searchText: String) { // Filter the array using the filter method filteredDomande = arrayDomande.filter({( domanda: Domanda) -> Bool in let StringMatch = domanda.testo.rangeOfString(searchText) return StringMatch != nil }) } func searchDisplayController(controller: UISearchDisplayController,shouldReloadTableForSearchString searchString: String!) -> Bool { self.filterContentForSearchText(searchString) return true } }
这是我的QuestionCellTableViewCell.swift
import UIKit class QuestionCellTableViewCell: UITableViewCell { @IBOutlet weak var testoLabel: UILabel! @IBOutlet weak var numeroLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSELEcted(SELEcted: Bool,animated: Bool) { super.setSELEcted(SELEcted,animated: animated) // Configure the view for the SELEcted state } }
这是我的Domanda.swift
import UIKit class Domanda: NSObject { var numero: Int = Int() var testo: String = String() var preferita: Bool = Bool() var visualizzato: Int = Int() var area: Int = Int() var argomento: Int = Int() }
一切正常,直到我在搜索栏中搜索一个字符串:应用程序崩溃,控制台返回:
使用调试我看到“单元格”存在,但其出口(testoLabel和numeroLabel)为零,因此应用程序崩溃
cell.testoLabel.text = domanda.testo
如果我在viewDidLoad()中删除
self.searchDisplayController!.searchResultsTableView.registerClass(QuestionCellTableViewCell.classForCoder(),forCellReusEIDentifier: "QuestionCell")
在搜索栏中输入一些文本时,应用程序崩溃,执行时出现相同的错误
var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell
如果我添加viewDidLoad()
self.questionTableView.registerClass(QuestionCellTableViewCell.classForCoder(),forCellReusEIDentifier: "QuestionCell")
应用程序继续崩溃在cell.testoLabel.text但这次是在启动时.
给我一些建议?
非常感谢,我感谢任何帮助!
安德里亚
编辑:
今天我做了其他测试,结果如下:如果我将searchResultsTableView的单元格用作Basic单元格,一切正常,但如果我尝试使用自定义单元格的出口,则应用程序崩溃.这是工作代码:
func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell var domanda = Domanda() if tableView == self.searchDisplayController!.searchResultsTableView { domanda = filteredDomande[indexPath.row] cell.textLabel.text = domanda.testo } else { domanda = arrayDomande[indexPath.row] cell.testoLabel.text = domanda.testo cell.numeroLabel.text = String(domanda.numero) } return cell }
var cellIdentifier = "questionCell" @IBOutlet weak var questionTableView: UITableView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,aboutId: argomentoDomande.id) var nib = UINib(nibName: "QuestionCellTableViewCell",bundle: nil) self.searchDisplayController!.searchResultsTableView.registerNib(nib,forCellReusEIDentifier: cellIdentifier) self.questionTableView.registerNib(nib,forCellReusEIDentifier: cellIdentifier) } func tableView(tableView: UITableView,cellForRowATindexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: QuestionCellTableViewCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as QuestionCellTableViewCell var domanda = Domanda() if tableView == self.searchDisplayController!.searchResultsTableView { domanda = filteredDomande[indexPath.row] } else { domanda = arrayDomande[indexPath.row] } cell.testoLabel.text = domanda.testo cell.numeroLabel.text = String(domanda.numero) return cell }
以上是大佬教程为你收集整理的iOS Swift – 过滤具有搜索栏和搜索显示的自定义单元格的UITableView内容全部内容,希望文章能够帮你解决iOS Swift – 过滤具有搜索栏和搜索显示的自定义单元格的UITableView内容所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。