ios - 如何将 UISwitch 添加到 Uitableview

标签 ios swift uitableview

我找不到任何方法在我的表格 View 中添加切换按钮

我有一个完美运行的搜索栏,

现在我想添加一个切换按钮以在搜索中启用“AND”“OR”过滤器。

inline

inline

inline

import Foundation
import UIKit

class TableViewController: UITableViewController, UISearchResultsUpdating {
    var appDel: AppDelegate!
//    var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "my.png")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "my.png"))

    var filteredTableData = [String]()
    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        self.edgesForExtendedLayout = UIRectEdge.None
        UIApplication.sharedApplication().statusBarHidden = true
        appDel = UIApplication.sharedApplication().delegate as? AppDelegate
//        tabBarItem = customTabBarItem

        self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()

            self.tableView.tableHeaderView = controller.searchBar

            return controller
        })()

        // Reload the table
        self.tableView.reloadData()
        print("TableView called!!")
    }

    func updateSearchResultsForSearchController(searchController: UISearchController)
    {
        //refer to http://www.ioscreator.com/tutorials/add-search-table-view-tutorial-ios8-swift
        filteredTableData.removeAll(keepCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
        let array = ((appDel.dataFetcher?.appTitles)! as NSArray).filteredArrayUsingPredicate(searchPredicate)
        filteredTableData = array as! [String]
        self.tableView.reloadData()
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (self.resultSearchController.active) {
            return self.filteredTableData.count
        }
        else if appDel.dataFetcher?.appTitles == nil {
            return 0
        }else{
            return (appDel.dataFetcher?.appTitles?.count)!
        }
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("appCell", forIndexPath: indexPath)
        let currentItem: String = (appDel.dataFetcher?.appTitles?[indexPath.row])!

        if (self.resultSearchController.active) {
            cell.textLabel?.text = filteredTableData[indexPath.row]
            return cell
        }
        else {
            cell.textLabel?.text = currentItem
            cell.detailTextLabel?.text = String(indexPath.row)
            return cell
        }


    }

}

最佳答案

通过将 UIView 拖到 tableView 的顶部(位于第一个原型(prototype)单元格的上方)来创建表头 View 。请参阅this question举个例子。

您可以在那里放置 UISwitchView 并创建 VC 的 socket 。

关于ios - 如何将 UISwitch 添加到 Uitableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38934359/

相关文章:

ios - TableView 数据被覆盖

swift - 无法在 swift 中使用 getter 和 setter 设置变量本身

ios - 每个 UITableView 的 UITableView 循环有不同的数据?

ios - 将 IndexPath 行值传递给委托(delegate)函数而不是 Sender.tag 以删除图像项目 CollectionView Swift

ios - 了解 XCode 中 Watch-iPhone 应用程序的构建

android - Azure AD Graph API - 在 iOS/Android 上获取 token

iphone - 在 Iphone SDK 中更改网页 View 的字体大小

ios - 当应用程序在后台且本地通知触发时执行代码

ios - 如何使用 Stackview 消费整个 View

ios - 如何声明弱通知