import
UIKit
class
ViewController
:
UIViewController
,
UISearchBarDelegate
,
UITableViewDataSource
,
UITableViewDelegate
{
@IBOutlet
var
searchBar :
UISearchBar
!
@IBOutlet
var
tableView :
UITableView
!
var
ctrls:[
String
] = [
"Label"
,
"Button1"
,
"Button2"
,
"Switch"
]
var
ctrlsel:[
String
] = []
override
func
viewDidLoad() {
super
.viewDidLoad()
self
.ctrlsel =
self
.ctrls
self
.searchBar.delegate =
self
self
.tableView.delegate =
self
self
.tableView.dataSource =
self
self
.tableView.registerClass(
UITableViewCell
.
self
,
forCellReuseIdentifier:
"SwiftCell"
)
}
func
tableView(tableView:
UITableView
, numberOfRowsInSection section:
Int
) ->
Int
{
return
self
.ctrlsel.count
}
func
tableView(tableView:
UITableView
, cellForRowAtIndexPath indexPath:
NSIndexPath
)
->
UITableViewCell
{
let
identify:
String
=
"SwiftCell"
let
cell = tableView.dequeueReusableCellWithIdentifier(identify,
forIndexPath: indexPath)
as
UITableViewCell
cell.accessoryType =
UITableViewCellAccessoryType
.
DisclosureIndicator
cell.textLabel?.text =
self
.ctrlsel[indexPath.row]
return
cell
}
func
searchBar(searchBar:
UISearchBar
, textDidChange searchText:
String
) {
print
(searchText)
if
searchText ==
""
{
self
.ctrlsel =
self
.ctrls
}
else
{
self
.ctrlsel = []
for
ctrl
in
self
.ctrls {
if
ctrl.lowercaseString.hasPrefix(searchText.lowercaseString) {
self
.ctrlsel.append(ctrl)
}
}
}
self
.tableView.reloadData()
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
请发表评论