程序代码 如何添加对tableview数据的搜索
struct jsonstruct:Decodable {
var name:String
var Id : Int = 0
let MobileNumber:String
let gender:String
enum CodingKeys: String, CodingKey {
case name = "name"
case Id = "Id"
case gender = "gender"
}
}
class ProfileVC: UIViewController, UITableViewDelegate,
UITableViewDataSource , UISearchBarDelegate{
var isSearch : Bool = false
var arrFilter:[String] = []
@IBOutlet weak var tblView: UITableView!
var arrayData = [AnyObject]()
var arrdata = [jsonstruct]()
var ListArray : [[String : AnyObject]] = [[String : AnyObject]]()
@IBOutlet weak var searchBar: UISearchBar!
let url = "......"
override func viewDidLoad() {
super.viewDidLoad()
postmethod()
tblView.reloadData()
func postmethod(){
let params: [String : String] = ["Id" : "01"]
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key, value) in params
{
multipartFormData.append((value.data(using: .utf8))!,
withName: key)
}
},
to: url,
encodingCompletion: { encodingResult in switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
print(response.value as Any)
if let result = response.result.value {
if let jsonstruct = result as? NSDictionary {
let ListArray = jsonstruct.value(forKey: "List")
as! NSArray
let name = ListArray.value(forKey: "name") as!
NSArray
let Id = ListArray.value(forKey: "Id") as! NSArray
.......
.......
if let dict = response.result.value as? [String :
AnyObject], let list = dict["List"] as? [[String :
AnyObject]] {
self.arrayData = list as [AnyObject]
DispatchQueue.main.async {
self.tblView.reloadData()
}
}
for mainarr in self.arrdata{
print(mainarr.name,":",mainarr.Id,":",mainarr.gender,":".....)
DispatchQueue.main.async {
self.tblView.reloadData()
}
}
}
}
}
case .failure(_): break
}
}
)
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath
indexPath: NSIndexPath) {
}
func numberOfSections(in tableView: UITableView) -> Int
{
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section:
Int) -> Int {
if(isSearch) {
return self.arrFilter.count
}
return self.arrayData.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for:
indexPath) as! TableViewCell
var jsonDict = self.arrayData[indexPath.row] as? [String:AnyObject]
cell.lblname.text = jsonDict?["name"] as? String
cell.lblGender.text = jsonDict?["gender"] as? String
cell.lblId.text = jsonDict?["Id"] as? String
return cell
}
func configureCell(cell: UITableViewCell, forRowAtIndexPath:
NSIndexPath) {
if(isSearch){
cell.textLabel?.text = arrFilter[forRowAtIndexPath.row]
} else {
cell.textLabel?.text = arrayData[forRowAtIndexPath.row] as! String;
}
}
//MARK: UISearchbar delegate
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
isSearch = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
isSearch = false;
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
isSearch = false;
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
isSearch = false;
}
self.tblView.reloadData()
}
}
存储来自api的数据并在tableview中查看。但是我想使用名称和ID ...的搜索来过滤tableview中的数据。 怎么可能???
searchBar UISearchBar对象为栏上的搜索字段提供了用户界面,但是轻按按钮时,应用程序有责任执行操作。在文本字段中输入文本时,代表至少需要进行实际搜索