iOS Swift-过滤UITableView的内容,该内容具有带有搜索栏和搜索显示的自定义单元格
我对iPhone的Swift项目中的搜索栏和搜索显示有问题。我在视图中添加了一个带有自定义单元格的UITableView。单元格的索引器设置为“QuestionCell”,类设置为“QuestionCellTableViewCell”。我向该单元格添加了两个标签,并向单元格类添加了标签的插座。 我的目标是过滤UITableView的内容。 这是我的ViewController: DomandeViewController.swiftiOS Swift-过滤UITableView的内容,该内容具有带有搜索栏和搜索显示的自定义单元格,uitableview,swift,ios8,uisearchbar,custom-cell,Uitableview,Swift,Ios8,Uisearchbar,Custom Cell,我对iPhone的Swift项目中的搜索栏和搜索显示有问题。我在视图中添加了一个带有自定义单元格的UITableView。单元格的索引器设置为“QuestionCell”,类设置为“QuestionCellTableViewCell”。我向该单元格添加了两个标签,并向单元格类添加了标签的插座。 我的目标是过滤UITableView的内容。 这是我的ViewController: DomandeViewController.swift import UIKit class DomandeView
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
}
}
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
}
}
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()
}
这是我的问题单元格TableViewCell.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
}
}
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
}
}
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()
}
这是我的多曼达.斯威夫特
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
}
}
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
}
}
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()
}
一切正常,直到我在搜索栏中搜索字符串:应用程序崩溃,控制台返回:
“致命错误:在展开可选文件时意外发现零。”
“价值”
通过debug,我看到“cell”存在,但它的输出(testoLabel和numeriolabel)为零,因此应用程序会崩溃
cell.testoLabel.text = domanda.testo
如果我在viewDidLoad()中删除
当在搜索栏中输入某些文本时,应用程序崩溃,执行时出现相同错误
var cell = tableView.dequeueReusableCellWithIdentifier("QuestionCell") as QuestionCellTableViewCell
如果我在viewDidLoad()中添加
应用程序在cell.testoLabel.text处不断崩溃,但这次是在启动时
给我一些建议?
非常感谢,非常感谢您的帮助
安德烈
编辑:
今天我做了其他测试,结果如下:如果我使用searchResultsTableView的单元格作为基本单元格,一切正常,但如果我尝试使用自定义单元格的出口,应用程序将崩溃。这是工作代码:
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中,我为questionTableView将原型单元格设置为0,然后在viewDidLoad()中为questionTableView和searchResultsTableView注册了Nib forCellReuseIdentifier。这是我的代码:
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
}
您不应该再在IOS 8中使用searchDisplayController,因为它被标记为已弃用,并将在将来的版本中删除。请改用UiSearchController。谢谢Christian的建议!我在Interface Builder中使用了搜索栏和搜索显示控制器组件,默认情况下,它使用UISearchDisplayController作为类。我希望苹果会改变它在IB中的默认类!您需要手动卸下DisplayController。这有点垃圾。要明确的是,你的应用不会因为使用了不推荐的类而被拒绝,但我想它会随着iOS 9被删除。事实上,该应用被批准没有问题;)谢谢你的提示,我开始删除不推荐的类!再次感谢克里斯蒂安!