如何在 Swift 中使用搜索栏过滤具有自定义单元格的 UITableView 的内容?

How to filter contents of UITableView that has custom cell with Search Bar in Swift?

我正在使用 swift 和 Xcode 版本 6.3

构建 iOS 应用程序

我创建了一个 tableview,searchbar 作为 outlet。

之后,我创建了一个新的 class ProjectTableviewCell 作为 UITableview 单元格的子class。

在我的故事板中,我将多个标签连接到原型单元格。

我实现了搜索栏 methods.And 它在 tableviewcell 中过滤数据 properly.This 一切正常。

默认情况下,在我搜索任何内容之前,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.Your 的初学者,将不胜感激。

修改代码如下。

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
    }