
[英]How to filter contents of UITableView that has custom cell with Search Bar in Swift?
[英]iOS Swift - Filtering contents of UITableView that has custom cell with Search Bar and Search Display
我在iPhone的Swift项目中遇到了搜索栏和搜索显示的问题。 我在View中添加了一个带有自定义单元格的UITableView。 Cell将Indetifier设置为“QuestionCell”,将Class设置为“QuestionCellTableViewCell”。 我在那个单元格中添加了两个标签,我为标签添加了出口到细胞类。 我的目标是过滤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()
}
一切正常,直到我在搜索栏中搜索一个字符串:应用程序崩溃,控制台返回:
“致命错误:在展开Optional值时意外发现nil”
使用调试我看到“单元格”存在,但其出口(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
}
最后我似乎无法使用我的自定义单元格与searchResultsTableView。 我的错误在哪里? 再次感谢你!
最后我找到了解决方案。 我在我的项目中添加了一个新的用户界面空文件,然后我在这个新的nib文件中创建了一个单元格,并为其分配了我的自定义单元格类。 在Interface Builder中,我将prototype Cells设置为0,用于questionTableView,然后我在viewDidLoad()中注册了Nib forCellReuseIdentifier,用于searchResultsTableView的questionTableView。 这是我的代码:
var cellIdentifier = "questionCell"
@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)
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.