我正在使用uitableview来显示我生成的一些数据。数据被分成特定的类,然后在每个单元格中使用。当页面加载时,它显示一个空白的表视图,但是一旦我点击搜索栏并键入一个字母,单元格就会出现在屏幕上。我相信这可能是一个异步问题?我想发布一张图片,但我需要10个代表点:( 我应该这样,只有当我从服务器上获取数据时才会发生这种情况。当我硬编码数据时,它会在初始加载屏幕上显示它
代码:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredProducts.count
} else {
return self.products.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//ask for a reusable cell from the tableview, the tableview will create a new one if it doesn't have any
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!
var product : Product
// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array
if tableView == self.searchDisplayController!.searchResultsTableView {
product = filteredProducts[indexPath.row]
} else {
product = products[indexPath.row]
}
// Configure the cell
cell.textLabel!.text = product.title
println(product.title)
println(cell)
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
self.filteredProducts = self.products.filter(
{
( product : Product) -> Bool in
var categoryMatch = (scope == "All") || (product.category == scope)
var stringMatch = product.title.rangeOfString(searchText)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool {
let scopes = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
let selectedScope = scopes[self.searchDisplayController!.searchBar.selectedScopeButtonIndex] as String
self.filterContentForSearchText(searchString, scope: selectedScope)
return true
}
func searchDisplayController(controller: UISearchDisplayController!,
shouldReloadTableForSearchScope searchOption: Int) -> Bool {
let scope = self.searchDisplayController!.searchBar.scopeButtonTitles as [String]
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text, scope: scope[searchOption])
return true
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("productDetail", sender: tableView)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "productDetail" {
let productDetailViewController = segue.destinationViewController as ProductDetailViewController
if sender as UITableView == self.searchDisplayController!.searchResultsTableView {
let indexPath = self.searchDisplayController!.searchResultsTableView.indexPathForSelectedRow()!
let destinationTitle = self.filteredProducts[indexPath.row].title
productDetailViewController.title = destinationTitle
productDetailViewController.productPriceText = String(self.products[indexPath.row].title)
productDetailViewController.productTitleText = self.products[indexPath.row].title
productDetailViewController.productDescriptionText = self.products[indexPath.row].description
} else {
let indexPath = self.tableView.indexPathForSelectedRow()!
let destinationTitle = self.products[indexPath.row].title
productDetailViewController.title = destinationTitle
}
}