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

如何使用Swift添加Table View搜索框,第1张

Xcode6新建一个项目,采用swift创建代码:

创建一个ViewController继承UITableViewController

涉及了模型控制器

模型:ZLPlace.swift

class 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)  

        }  

    }  

      

}

效果如图:

//appdelegate里初始化独立的:

self.window?.rootViewController = UINavigationController(rootViewController: ViewController())

//导航上的左系统按钮:

self.navigationItem.leftBarButtonItem = UIBarButtonItem (barButtonSystemItem:        UIBarButtonSystemItem.camera, target: self, action: #selector(add(sender:)))

//自己写的导航上的按钮

let leftBtn = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(leftButton))

        self.navigationItem.leftBarButtonItem = leftBtn

//添加图片:

前面是:var img : UIImage?

        img = UIImageView(frame: CGRect(x: 160, y: 100, width: 80, height: 80))

        img?.image = UIImage(named: "未选中的副本")

        self.view.addSubview(img!)

//文本框

前面是:var text : UITextField? = nil

text = UITextField(frame: CGRect(x: 50, y: 250, width: 300, height: 50))

        text?.backgroundColor = .green

        //提示文字

        text?.placeholder = "222"

        self.view.addSubview(text!)

//button

        btn = UIButton(frame: CGRect(x: 70, y: 380, width: 250, height: 50))

        btn?.backgroundColor = .blue

        btn?.setTitle("12345", for: .normal)

        self.view.addSubview(btn!)

//表格

tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")

let identify = "SwiftCell"

let cell = tableView.dequeueReusableCell(withIdentifier: identify, for: indexPath)

或者带副标题的:(里面是这个,上面多的注了)

let identify = "SwiftCell"

        var cell = tableView.dequeueReusableCell(withIdentifier: identify)

        if cell == nil {

            cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier:identify)

        }

左右侧滑的

/// 标题

        let titles = ["推荐","快讯", "深度", "实点对话", "行情分析"]

        //样式

        let style = YCTitleStyle()

        //可以滚动

        style.isScrollEnable = false

        // 所以的子控制器

        var childVcs = [UIViewController]()

        for _ in 0..<titles.count {

            let vc = UIViewController()

            vc.view.backgroundColor = UIColor.white

            childVcs.append(vc)

        }

        // pageView的frame

        let pageFrame = CGRect(x: 0, y: 65, width: view.bounds.width, height: view.bounds.height - 110)

        // 创建YCPageView,并且添加到控制器的view中

        let pageView = YCPageView(frame: pageFrame, titles: titles, childVcs: childVcs, parentVc: self, style : style)

        view.addSubview(pageView)

        tbv = UITableView(frame: CGRect(x: 0, y: 110, width: self.view.frame.size.width, height: self.view.frame.size.height-100), style: .plain)

        tbv?.delegate = self

        tbv?.dataSource = self

        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")

        self.view.addSubview(tbv!)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11245118.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存