Ios 如何在Swift中过滤具有自定义单元格和搜索栏的UITableView内容?
我正在使用swift和Xcode版本6.3构建iOS应用程序 我已经创建了一个tableview,搜索栏作为一个出口 之后,我创建了一个新类ProjectTableviewCell作为UITableview单元格的子类 在我的故事板中,我将多个标签连接到原型单元 我实现了searchbar方法。它在tableviewcell中正确地过滤数据。这一切都很好 默认情况下,在搜索任何内容之前,tableview显示内容列表 过滤操作后,它仅显示特定的过滤数组 我现在面临的问题是tableview单元格内容未显示 我的预期输出是,我希望在tableview中显示包含现有单元格内容的过滤数组Ios 如何在Swift中过滤具有自定义单元格和搜索栏的UITableView内容?,ios,swift,uitableview,uisearchbar,Ios,Swift,Uitableview,Uisearchbar,我正在使用swift和Xcode版本6.3构建iOS应用程序 我已经创建了一个tableview,搜索栏作为一个出口 之后,我创建了一个新类ProjectTableviewCell作为UITableview单元格的子类 在我的故事板中,我将多个标签连接到原型单元 我实现了searchbar方法。它在tableviewcell中正确地过滤数据。这一切都很好 默认情况下,在搜索任何内容之前,tableview显示内容列表 过滤操作后,它仅显示特定的过滤数组 我现在面临的问题是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的初学者。非常感谢您的帮助。修改了代码,如下所示
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
}
修改代码如下所示
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
}
建议创建一个项目类,而不是您所使用的数组数。是的,我刚才理解,我改为一个类。建议创建一个项目类,而不是您所使用的数组数。是的,我刚才理解,我改为一个类。