• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何在带有部分的 TableView 上创建搜索栏?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:04:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我需要一个用于表格 View 的搜索栏,但我是初学者,我正在努力做到这一点。

在我的表格 View 中,我有几个部分。每个部分都填充有包含名称和日期的项目。我希望用户能够通过名称进行研究。

还有一件事:为了使它更容易和更有条理,我创建了一个枚举并想用它来命名部分。

我已经做了一个例子来展示我已经达到了多远:

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。

提前致谢。



Best Answer-推荐答案


我建议你关注 Link

我在我的项目中使用过它,并且非常容易实现和维护。您必须管理两个数组结果:

1. Matched with search text

2. Without any search text

Reload the table section and rows overtime you type a text.

如果您在实现它时遇到任何问题,请告诉我。

关于ios - 如何在带有部分的 TableView 上创建搜索栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38532817/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap