如何在 Swift 中使用搜索栏过滤具有自定义单元格的 UITableView 的内容?
Posted
技术标签:
【中文标题】如何在 Swift 中使用搜索栏过滤具有自定义单元格的 UITableView 的内容?【英文标题】:How to filter contents of UITableView that has custom cell with Search Bar in Swift? 【发布时间】:2015-07-15 09:56:42 【问题描述】:我正在使用 swift 和 Xcode 6.3 版构建 ios 应用程序
我创建了一个表格视图,搜索栏作为一个出口。
之后我创建了一个新类 ProjectTableviewCell 作为 UITableview 单元格的子类。
在我的故事板中,我将多个标签连接到原型单元格。
我实现了搜索栏方法。它在 tableviewcell 中正确过滤数据。这一切都很好。
默认情况下,在我搜索任何内容之前,tableview 会显示内容列表。
过滤操作后,它只显示特定的过滤数组。
我现在面临的问题是表格视图单元格内容没有显示。
我的预期输出是我想在我的表格视图中显示过滤后的数组以及现有的单元格内容。
class myProjects: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate
@IBOutlet weak var projectTable: UITableView!
@IBOutlet weak var searchProject: UISearchBar!
var project = [String]()
var searchActive : Bool = false
var filtered:[String] = []
//Search Bar Methods
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
searchActive = true;
func searchBarTextDidEndEditing(searchBar: UISearchBar)
searchActive = false;
func searchBarCancelButtonClicked(searchBar: UISearchBar)
searchActive = false;
func searchBarSearchButtonClicked(searchBar: UISearchBar)
searchActive = false;
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
filtered = project.filter( (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
)
if(filtered.count == 0)
searchActive = false;
else
searchActive = true;
self.projectTable.reloadData()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
if(searchActive)
return filtered.count
return project.count;
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let textCellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! projectTableViewCell
let row = indexPath.row
if(searchActive)
cell.textLabel?.text = filtered[indexPath.row]
else
(cell.contentView.viewWithTag(1) as! UILabel).text = project[row] as String
(cell.contentView.viewWithTag(3) as! UILabel).text = projectStatus[row] as String
(cell.contentView.viewWithTag(7) as! UILabel).text = ProjectDescription[row] as String
(cell.contentView.viewWithTag(12) as! UILabel).text = category[row] as String
(cell.contentView.viewWithTag(13) as! UILabel).text = subcategory[row] as String
(cell.contentView.viewWithTag(14) as! UILabel).text = project_Tags[row] as String
(cell.contentView.viewWithTag(15) as! UILabel).text = skill_Needed[row] as String
(cell.contentView.viewWithTag(16) as! UILabel).text = budget[row] as String
(cell.contentView.viewWithTag(17) as! UILabel).text = timeFrames[row] as String
(cell.contentView.viewWithTag(18) as! UILabel).text = location[row] as String
return cell
我是 Swift 初学者。非常感谢您的帮助。
【问题讨论】:
建议做一个Class的项目,而不是你拿的这些数的数组。 是啊,我刚才明白了,我换了一个班级。 【参考方案1】:修改代码如下所示。
struct Project
var title = ""
var current_state = ""
var description = ""
var category_name = ""
var subcategory_name = ""
var tags = ""
var skills = ""
var budget = ""
var estimated_time = ""
var location = ""
init(title: String, current_state: String, description: String, category_name: String, subcategory_name: String, tags: String, skills: String, budget: String, estimated_time: String, location: String)
self.title = title
self.current_state = current_state
self.description = description
self.category_name = category_name
self.subcategory_name = subcategory_name
self.tags = tags
self.skills = skills
self.budget = budget
self.estimated_time = estimated_time
self.location = location
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let textCellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! projectTableViewCell
let row = indexPath.row
if(searchActive)
(cell.contentView.viewWithTag(1) as! UILabel).text = filtered[row].title as String
else
(cell.contentView.viewWithTag(1) as! UILabel).text = projectsArray[row].title as String
(cell.contentView.viewWithTag(3) as! UILabel).text = projectsArray[row].current_state as String
(cell.contentView.viewWithTag(7) as! UILabel).text = projectsArray[row].description as String
(cell.contentView.viewWithTag(12) as! UILabel).text = projectsArray[row].category_name as String
(cell.contentView.viewWithTag(13) as! UILabel).text = projectsArray[row].subcategory_name as String
(cell.contentView.viewWithTag(14) as! UILabel).text = projectsArray[row].tags as String
(cell.contentView.viewWithTag(15) as! UILabel).text = projectsArray[row].skills as String
(cell.contentView.viewWithTag(16) as! UILabel).text = projectsArray[row].budget as String
(cell.contentView.viewWithTag(17) as! UILabel).text = projectsArray[row].estimated_time as String
(cell.contentView.viewWithTag(18) as! UILabel).text = projectsArray[row].location as String
return cell
【讨论】:
以上是关于如何在 Swift 中使用搜索栏过滤具有自定义单元格的 UITableView 的内容?的主要内容,如果未能解决你的问题,请参考以下文章