2016-07-22 54 views
2

我需要一個搜索欄來查看我的表格,但我是一名初學者,我正在努力做到這一點。如何在帶有部分的TableView上創建SearchBar?

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

還有一件事:爲了使它更容易和更有組織,我創建了一個枚舉並希望用它來命名這些部分。

我做了一個例子來說明如何到目前爲止,我已經達到了:

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。

在此先感謝。

回答

0

我會reccomment您按照Link

我在我的項目中使用它,是很容易實現和維護。你將不得不管理兩個數組結果:

1.搜索文本

2.符合條件的無任何搜索文本

刷新的錶款和行加班鍵入一個文本。

讓我知道,如果你面對實現它的任何問題。

0

我會建議的是有一個電影和過濾的電影數組。數據源應該是過濾後的電影,默認情況下是電影。在搜索字段中搜索字段時,應根據搜索字段上的文本篩選符合條件的電影陣列。然後,你應該重新加載tableview的數據,你的搜索項目只應該出現。根據你想要實現它的方式,每當你改變你的文本框或者當它停止編輯或者當它返回時都可以完成。

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。

相關問題