簡體   English   中英

如何在具有部分的TableView上創建SearchBar?

[英]How do I create a SearchBar on a TableView with sections?

我需要一個搜索欄來查看其表格視圖,但是我是個初學者,因此我正竭盡全力。

在表格視圖中,我有幾個部分。 每個部分都填充有包含名稱和日期的項目。 我希望用戶能夠通過該名稱進行研究。

還有一件事:為了使它更容易和更有條理,我創建了一個枚舉,並希望使用它來命名各節。

我舉了一個例子來說明我已經走了多遠:

class Cinema: UITableViewController {

// MARK: - Properties

var movie:[(type: Genre, data:[Movies])] = []

//MARK: - Super Methods
override func viewDidLoad() {
    super.viewDidLoad()

    movie = [

        (Genre.Action, [

            Movies (name: "Matrix", year: "1999", description: "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers."),

            Movies (name: "Gladiator", year: "2000", description: "When a Roman general is betrayed and his family murdered by an emperor's corrupt son, he comes to Rome as a gladiator to seek revenge."),

            Movies (name: "Saving Private Ryan", year: "1998", description: "Following the Normandy Landings, a group of U.S. soldiers go behind enemy lines to retrieve a paratrooper whose brothers have been killed in action.")
            ]),

        (Genre.Drama, [

            Movies (name: "Good Will Hunting", year: "1997", description: "Will Hunting, a janitor at M.I.T., has a gift for mathematics, but needs help from a psychologist to find direction in his life."),

            Movies (name: "Schindler's List", year: "1993", description: "In Poland during World War II, Oskar Schindler gradually becomes concerned for his Jewish workforce after witnessing their persecution by the Nazis.")
            ]),

        (Genre.Comedy, [

            Movies (name: "Monty Python and the Holy Grail", year: "1975", description: "King Arthur and his knights embark on a low-budget search for the Grail, encountering many, very silly obstacles."),

            Movies (name: "Hangover", year: "2009", description: "Three buddies wake up from a bachelor party in Las Vegas, with no memory of the previous night and the bachelor missing. They make their way around the city in order to find their friend before his wedding.")

            ])
    ]


}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return movie.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return movie[section].data.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ADTableViewCell

    cell.title.text = movie[indexPath.section].data[indexPath.row].name
    cell.year.text = movie[indexPath.section].data[indexPath.row].year

    return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Action"
    } else if section == 1 {
        return "Drama"
    } else {
        return "Comedy"
    }
}

}

PS:當用戶單擊他選擇的行時,會將其發送到UIViewController,其中將顯示名稱,年份和描述。 在此示例中,我沒有在此處創建prepareForSegue。

提前致謝。

我建議您關注此鏈接

我在項目中使用了它,並且非常容易實現和維護。 您將必須管理兩個數組結果:

1.與搜索文字匹配

2.沒有任何搜索文字

鍵入文本,重新加載表部分和加班的行。

讓我知道您是否在實施它時遇到任何問題。

我建議的是擁有一系列電影和已過濾電影。 數據源應該是經過篩選的電影,默認情況下是電影。 在帶有搜索字段的字段中搜索時,應根據搜索字段上的文本來過濾電影陣列。 然后,您應該重新加載表格視圖的數據,並且只顯示搜索項。 取決於您要如何實現它,可以在每次更改文本字段時,停止編輯時或返回時完成。

var filteredMovie:[(type: Genre, data:[Movies])] = []

func yourTextfieldDelegate(textField: Textfield) {
    if textField.text.characters.count == 0{
        self.filteredMovie = self.movie
    }
    else{
        // for example, filter by name
        self.filteredMovie = movie.filter($0.data.name.lowercase.contains(textfield.text.lowercase))
        self.tableViewRef.reloadData()
    }

}

ps:您缺少tableView的插座。 我稱它為tableViewRef。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM