我在通过表格视图搜索项目时遇到问题。 问题在于,当我搜索某些内容时,应用程序会显示一个新的表视图,其结果是在非常小的单元格中,无法看到任何内容(2个标签和1个imageView)。所以我的问题是如何在searchResultsTableView中调整显示的单元格高度,宽度和配置其他元素。 如果它有助于解决问题,我可以为您提供我的代码。
提前致谢
这是我的代码:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.filteredCandies.count
} else {
return self.candies.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CellTableViewCell
var candy : Candy
// 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 {
candy = filteredCandies[indexPath.row]
} else {
candy = candies[indexPath.row]
}
cell.label1.text = candy.name
//cell.label2.text = candy.category
cell.contentView.frame = cell.bounds
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
let item = candies[indexPath.row]
cell.setCell(item.name, imageName: item.imageName)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
var candy : Candy
if (tableView == self.searchDisplayController?.searchResultsTableView)
{
candy = self.filteredCandies[indexPath.row]
}
else
{
candy = self.candies[indexPath.row]
}
let infoViewController: InfoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("InfoViewController") as! InfoViewController
infoViewController.label01 = candy.name
infoViewController.label02 = candy.category
infoViewController.imageFile = candy.imageName
self.presentViewController(infoViewController, animated: true, completion: nil)
}
func filterContentForSearchText(searchText: String, scope: String = "All") {
// Filter the array using the filter method
self.filteredCandies = self.candies.filter({( candy: Candy) -> Bool in
let categoryMatch = (scope == "All") || (candy.category == scope)
let stringMatch = candy.name.rangeOfString(searchText.lowercaseString)
return categoryMatch && (stringMatch != nil)
})
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool {
self.filterContentForSearchText(searchString)
return true
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchScope searchOption: Int) -> Bool {
self.filterContentForSearchText(self.searchDisplayController!.searchBar.text)
return true
}
答案 0 :(得分:0)
我解决了。我只需要添加这一行:
searchDisplayController?.searchResultsTableView.rowHeight = 100
viewDidLoad中的和单元格大小已更改。