rangeofstring

时间:2024-10-20 08:07:36编辑:阿星

如何给TableView添加搜索栏

在Xcode中,选择 File\New\Project… 创建一个新项目。选择 Single View Application 点 Next 。将项目命名为 CandySearch 确保 Language 设置为 Swift 并且 Devices 设置为 iPhone 。点击完成,选择你想储存的位置,然后点击 Create 。
首先将默认的文件清理掉,这样你就可以真正的从头开始。在 Project Navigator 中,选择 ViewController.swift ,右键点击,选择删除,然后选择 Move to Trash 。然后打开 Main.storyboard ,选择唯一的一个view controller然后删掉他。现在你有一个空的storyboard,可以在你的应用中添加主屏幕了。
从 Object Browser (边框控制条的右下部分)拖出 Navigation Controller 以将iOS内置的导航逻辑添加到项目中。这样可以在storyboard中创建两个view controllers – navigation controller和table view controller,这些将作为应用的初始视图。
当用户选择列表中的某一项时,你需要一个视图控件来显示详细的内容。从Table View Controller中拖拽,一直到那个新的view controller释放,并且在弹出的菜单中选择 show 来作为manual segue的选项。
设置糖果类
下一步你将创建一个用来显示糖果的数据结构,保存像策略和名称类的信息。在 CandySearch 文件夹上点击右键,然后选择 New File… 。选择 iOS \ Source \ Swift File 然后点击下一步。将此文件命名为 Candy.swift 。打开文件,然后添加如下内容:
struct Candy {

let category : String

let name : String

}

这个结构有两个属性:糖果的策略和名称。当你的用户在应用中搜索糖果时,会引用名称属性对用户搜索的字符串进行比较。你文章稍后将会发现策略字符串在你实现分类条时有多么重要。
你不需要在这里添加自己的初始化,你能得到一个自动生成的机制。默认的,初始化参数将对属性进行配置。在下一个部分中你将看到如何创建 candy 实例。
现在你已经准备好设置 UITableView 以使你的 UISearchBar 能够过滤信息!
连接Table View
下一步你将设置 UITableView 让他和 UISearchBar 一起工作。右键点击 CandySearch 文件夹并且选择 New File… 。选择 iOS \ Source \ Cocoa Touch Class 点击下一步。类命名为 CandyTableViewController ,父类设置为 UITableViewController 并且设置语言为 Swift 。
开始的时候我们需要添加一个数组。打开 CandyTableViewController.swift 并且增加如下代码:
var candies = [Candy]()

candies 数组将会管理所有不同的 Candy 对象,以便用户搜索。说到这里,是时候创建你的糖果了!在这篇引导中,你只需要创建一个数量区间,用来说明搜索条如何工作;在制作应用的过程中,你会发现你有成千上万的条目需要检索。但是不论你的应用程序有多少条目,搜索的方法都是相同的。具有可伸缩性是最好的!为了能够布置好你的 candies 数组,重写 viewDidLoad 如下:
override func viewDidLoad() {

super.viewDidLoad()

// 向candyArray中添加简单的数据

self.candies = [Candy(category:"Chocolate", name:"chocolate Bar"),

Candy(category:"Chocolate", name:"chocolate Chip"),

Candy(category:"Chocolate", name:"dark chocolate"),

Candy(category:"Hard", name:"lollipop"),

Candy(category:"Hard", name:"candy cane"),

Candy(category:"Hard", name:"jaw breaker"),

Candy(category:"Other", name:"caramel"),

Candy(category:"Other", name:"sour chew"),

Candy(category:"Other", name:"gummi bear")]

// 刷新table

self.tableView.reloadData()

}


如何使用Swift添加Table View搜索框

Xcode6新建一个项目,采用swift创建代码:创建一个ViewController继承UITableViewController涉及了模型,控制器模型:ZLPlace.swiftclass ZLPlace: NSObject { var place = "" var visited = false } tableViewController 控制器import UIKit class ViewController: UITableViewController { // 静态数据数组,存放模型 var arrs = [ZLPlace]() override func viewDidLoad() { super.viewDidLoad() let place2 = ZLPlace() place2.place = "zhang2" arrs.append(place2) let place3 = ZLPlace() place3.place = "zhang3" arrs.append(place3) let place4 = ZLPlace() place4.place = "zhang1" arrs.append(place4) self.tableView.reloadData() } // 数据源方法, 返回多少组 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1; } // 每组有多少行 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrs.count; } // 每行展示什么内容 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell let place = arrs[indexPath.row] cell.textLabel.text = place.place return cell; } // 点击每个cell触发什么事件 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let place = arrs[indexPath.row] place.visited = !place.visited; let cell = tableView.cellForRowAtIndexPath(indexPath) cell?.backgroundColor = UIColor.clearColor() if(place.visited){ cell?.accessoryType = UITableViewCellAccessoryType.Checkmark }else{ cell?.accessoryType = UITableViewCellAccessoryType.None } } // 点击编辑按钮 @IBAction func editing(sender: AnyObject) { self.tableView.setEditing(true, animated: true) } // 删除每个cell override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == UITableViewCellEditingStyle.Delete{ arrs.removeAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top) } } } 效果如图:

上一篇:快乐多一些

下一篇:没有了