如何在带有部分的 TableView 上创建 SearchBar?
Posted
技术标签:
【中文标题】如何在带有部分的 TableView 上创建 SearchBar?【英文标题】:How do I create a SearchBar on a TableView with sections? 【发布时间】:2016-07-22 18:07:05 【问题描述】:我需要一个用于表格视图的搜索栏,但我是初学者,我正在努力做到这一点。
在我的表格视图中,我有几个部分。每个部分都填充有包含名称和日期的项目。我希望用户能够通过名称进行研究。
还有一件事:为了使它更容易和更有条理,我创建了一个枚举并想用它来命名部分。
我已经做了一个例子来展示我已经达到了多远:
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"
P.S:当用户点击他选择的行时,他将被发送到一个 UIViewController,该 UIViewController 将显示名称、年份和描述。在此示例中,我没有在此处创建 prepareForSegue。
提前致谢。
【问题讨论】:
【参考方案1】:我建议你关注这个Link
我在我的项目中使用过它,并且非常容易实现和维护。您必须管理两个数组结果:
1.与搜索文本匹配
2。没有任何搜索文字
在您输入文本时重新加载表格部分和行。
如果您在实施它时遇到任何问题,请告诉我。
【讨论】:
【参考方案2】:我的建议是拥有一系列电影和过滤后的电影。数据源应该是过滤后的电影,默认情况下是电影。使用搜索字段搜索字段时,您应该根据搜索字段上的文本按照您的条件过滤电影数组。然后,您应该重新加载 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。
【讨论】:
以上是关于如何在带有部分的 TableView 上创建 SearchBar?的主要内容,如果未能解决你的问题,请参考以下文章
UIButton 状态在滚动带有多个部分的 tableview 时发生变化 - Swift
带有核心数据的 TableView:从 windowShouldClose 调用 saveAction