Together项目IOS平台开发09

上次我做了一个简单的搜索页面。但是说实话,这个搜索界面连基本的搜索功能差强人意,因此我这次在上次的基础上改良了下。首先,我用了一个数组作为演示的数据。为了让整个界面显示的效果有些不同,我给之前的NavigationController,稍微修改了下颜色,包括下面的TabBar也修改了下眼色。这个地方搜索所用的整体的框架是从一个发现的SearchViewController视图的右导航按钮开始的。通过点击位于NavigationBar右侧的图标按钮,来进入搜索按钮。但是我的SearchViewController暂时还没有设计,因此这里现在是空白,后期会添加一些类似推荐栏一样的内容。这里有个比较严重的bug是在不按Cancel按钮返回发现栏时,键盘仍然会出现,并没有消失,这个问题在后期会修好。

以下是代码部分

1、SearchViewController

//
//  SearchViewController.swift
//  FinalTest
//
//  Created by 沈力同 on 2017/5/10.
//  Copyright © 2017年 沈力同. All rights reserved.
//

import UIKit

class SearchViewController: UIViewController {
    let screenSizeWidth = UIScreen.main.bounds.size.width
    
    let screenSizeHeight = UIScreen.main.bounds.size.height
    
    override func viewDidLoad() {
        super.viewDidLoad()
   
//        let item=UIBarButtonItem(title: "搜索", style: UIBarButtonItemStyle.plain, target: self, action: #selector(LogViewController.click_register))

        var img1=UIImage(named: "search")
        let item=UIBarButtonItem(image:img1, style: UIBarButtonItemStyle.plain, target: self, action: #selector(LogViewController.click_register))
        self.navigationItem.rightBarButtonItem=item
        self.navigationController?.navigationBar.tintColor = UIColor.black
        
    }
    
    func click_register(){
        
        let SearchButtonView = SearchButtonViewController()
        self.navigationController?.pushViewController(SearchButtonView, animated: true)
    }
    

2、SearchButtonViewController

//
//  SearchButtonViewController.swift
//  FinalTest
//
//  Created by 沈力同 on 2017/6/8.
//  Copyright © 2017年 沈力同. All rights reserved.
//

import UIKit

class SearchButtonViewController: UIViewController{
    
    let screenSizeWidth = UIScreen.main.bounds.size.width
    
    let screenSizeHeight = UIScreen.main.bounds.size.height
    

    
    //展示列表
    var tableView: UITableView!
    
    //搜索控制器
    var countrySearchController = UISearchController()
    
    //原始数据集
    let dataArray = ["1","2","3","4","5",
                       "12","13","14","15",
                       "16","22","211","1234","1223",
                       "1333","1313","4443","12345"]
    
    //搜索过滤后的结果集
    var searchArray:[String] = [String](){
        didSet  {self.tableView.reloadData()}
    }
    override func loadView() {
        super.loadView()
        self.view.backgroundColor = UIColor.blue
        
        //创建表视图
        let tableViewFrame = CGRect(x: 0, y: 0, width:screenSizeWidth,
                                    height: screenSizeHeight)
        tableView = UITableView(frame: tableViewFrame, style:.plain)
        tableView!.delegate = self
        tableView!.dataSource = self
        tableView!.separatorStyle = UITableViewCellSeparatorStyle.singleLine;
        tableView!.separatorColor = UIColor.clear;
        //创建一个重用的单元格
        tableView!.register(UITableViewCell.self,
                                 forCellReuseIdentifier: "MyCell")
        self.view.addSubview(self.tableView!)
        
        //配置搜索控制器
        self.countrySearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self   //两个样例使用不同的代理
            controller.hidesNavigationBarDuringPresentation = false
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.searchBarStyle = .minimal
            controller.searchBar.sizeToFit()
            self.tableView.tableHeaderView = controller.searchBar
            
            return controller
        })()
        
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)
        self.tableView.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

3、SearchButtonViewControllerExtensions

//
//  SearchButtonViewController.swift
//  FinalTest
//
//  Created by 沈力同 on 2017/6/5.
//  Copyright © 2017年 沈力同. All rights reserved.
//

import Foundation
import UIKit

extension SearchButtonViewController: UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.countrySearchController.isActive {
            return self.searchArray.count
        } else {
//            return self.schoolArray.count
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
        -> UITableViewCell {
            //为了提供表格显示性能,已创建完成的单元需重复使用
            let identify:String = "MyCell"
            //同一形式的单元格重复使用,在声明时已注册
            let cell = tableView.dequeueReusableCell(withIdentifier: identify,
                                                     for: indexPath)
            
            if self.countrySearchController.isActive {
                cell.textLabel?.text = self.searchArray[indexPath.row]
                return cell
            } else {
                cell.textLabel?.text = self.dataArray[indexPath.row]
                return cell
            }
    }
}

extension SearchButtonViewController: UITableViewDelegate
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        tableView.deselectRow(at: indexPath, animated: true)
        self.countrySearchController.isActive = false
        let DetailView = DetailViewController()
        self.navigationController?.pushViewController(DetailView, animated: true)
    }
}

extension SearchButtonViewController: UISearchResultsUpdating
{
    //实时进行搜索
    func updateSearchResults(for searchController: UISearchController) {
        self.searchArray = self.dataArray.filter { (school) -> Bool in
            return school.contains(searchController.searchBar.text!)
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值