我试图过滤行,当我点击某个字母时它会返回
unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard)
这是我的列表数据类:
//
// HomeTableViewController.swift
//
//
// Created by Wesley Mota on 06/06/16.
//
//
import UIKit
import Firebase
class HomeTableViewController: UITableViewController, UISearchBarDelegate, UISearchDisplayDelegate {
var ref = FIRDatabase.database().reference()
var nome: String!
var items = [GroceryItem]()
var sobrenome: String!
var filteredItems = [GroceryItem]()
var detailViewController: DetailViewController? = nil
@IBOutlet weak var questionTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
ref.child("Cadastro").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
var newItems = [GroceryItem]()
for item in snapshot.children {
let groceryItem = GroceryItem(snapshot: item as! FIRDataSnapshot)
newItems.append(groceryItem)
}
self.items = newItems
self.tableView.reloadData()
})
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
//return items.count
if (tableView == self.searchDisplayController?.searchResultsTableView){
return self.filteredItems.count
} else{
return items.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomTableViewCell
let groceryItem = items[indexPath.row]
var items1: GroceryItem
if (tableView == self.searchDisplayController?.searchResultsTableView) {
items1 = self.filteredItems[indexPath.row]
} else {
items1 = self.items[indexPath.row]
}
cell.nameLbl.text = groceryItem.nome
cell.lastNameLbl.text = groceryItem.sobrenome
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var items1: GroceryItem
if (tableView == self.searchDisplayController?.searchResultsTableView) {
items1 = self.filteredItems[indexPath.row]
} else {
items1 = self.items[indexPath.row]
}
print(items1.nome)
}
func filterContetnForSearchText(searchText: String, scope: String = "All") {
self.filteredItems = self.items.filter({ (friend: GroceryItem) -> Bool in
var categoryMatch = (scope == "All")
var stringMatch = friend.nome.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool
{
self.filterContetnForSearchText(searchString!, scope: "All")
return true
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool
{
self.filterContetnForSearchText(self.searchDisplayController!.searchBar.text!, scope: "All")
return true
}
}
这是我的CustomTableViewCell
//
// Labels.swift
// enquetepr
//
// Created by Wesley Mota on 06/06/16.
// Copyright © 2016 WesleyMota. All rights reserved.
//
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var lastNameLbl: UILabel!
}
与firebase数据库的类连接:
//
// SubmitConnection.swift
// enquetepr
//
// Created by Wesley Mota on 06/05/16.
// Copyright © 2016 WesleyMota. All rights reserved.
//
import Foundation
import Firebase
struct GroceryItem {
var data: NSData = NSData()
let key: String!
let nome: String!
let sobrenome: String!
let ref: FIRDatabaseReference?
// Initialize from arbitrary data
init(nome: String, key: String = "", sobrenome: String) {
self.key = key
self.nome = nome
self.sobrenome = sobrenome
self.ref = nil
}
init(snapshot: FIRDataSnapshot) {
key = snapshot.key
nome = snapshot.value!["nome"] as! String!
sobrenome = snapshot.value!["sobrenome"] as! String!
ref = snapshot.ref
}
func toAnyObject() -> AnyObject {
return [
"nome": nome,
"sobrenome": sobrenome,
]
}
}
当我点击某个字母>> Image
这是故事板单元格ID>> Image
我使用firebase来存储数据。它工作(加载)很好,但是当我尝试过滤时发生了这个错误。
答案 0 :(得分:4)
有一些事情可能发生,但首先进入故事板并点击定义“CustomTableViewCell”的单元格。
确保单元格的类设置为“CustomTableViewCell”。 Set the class of a custom UITableViewCell
然后确保您的小区标识符设置在此处: Set a cell identifyer in storyboard
现在我们需要注册单元格。这是调试器抱怨的具体内容。
在HomeTableViewController的viewDidLoad方法中,添加以下代码以使用标识符注册类。
tableView.registerClass(CustomTableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
答案 1 :(得分:1)
试试这个,它对我有用,
检查以下方案是否存在此错误,
- 您可以在故事板中将表格单元格标识符设置为“单元格”吗?如果是,则只需在 viewDidLoad()
中添加一行代码 self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
OR
self.tableView.register(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "Cell")
确保在故事板的UITableViewCell中复制相同的标识符“Cell”。
希望所以这对某些人有用。
答案 2 :(得分:0)
答案 3 :(得分:0)
好吧,我不确定你是如何配置你的搜索控制器但是如果你用另一个控制器初始化了控制器,你必须在每次结果VC出现在屏幕上时以编程方式注册你的单元格。
init(searchResultsController searchResultsController: UIViewController?)
如果searchResultsController!= nil
,则为true您可以使用以下方法注册单元格:
func willPresentSearchController(_ searchController: UISearchController)