如何在TableView中处理选择行

时间:2015-02-26 02:09:05

标签: ios xcode uitableview swift tableview

在这个应用程序中,当您选择第二个选项“绿色”时,它将打印“绿色”,但是当您搜索绿色并且它出现在索引0处,并且您单击它时,它将打印“红色”。如何让它打印“绿色”无论在表格视图中哪个索引和行绿色?

感谢...

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {


    @IBOutlet weak var tableView: UITableView!

    let sampleData:[String] = ["red", "green", "blue", "brown", "black", "white", "purple", "silver"]
    var dataToDisplay:[String]!

    var searchController:UISearchController!




    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.dataToDisplay = self.sampleData

        self.tableView.delegate = self
        self.tableView.dataSource = self


        self.searchController = UISearchController(searchResultsController: nil)

        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self
        self.searchController.searchResultsUpdater = self
        self.searchController.searchBar.sizeToFit()
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.dimsBackgroundDuringPresentation = false



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataToDisplay.count
    }



    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Basic") as UITableViewCell

        cell.textLabel?.text = self.dataToDisplay[indexPath.row]

        return cell

    }


    func updateSearchResultsForSearchController(searchController: UISearchController) {

        let searchString:String = searchController.searchBar.text

        self.dataToDisplay = self.sampleData.filter ({ (dataString:String) -> Bool in

            let match = dataString.rangeOfString(searchString)

            if match != nil {
            return true
            }
            else {
            return false
            }





        })

        self.tableView.reloadData()




    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        self.tableView.hidden = false
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        self.tableView.hidden = false
    }

    func didDismissSearchController(searchController: UISearchController) {
        self.tableView.hidden = false
    }





 func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

        if indexPath.row == 1 {

        println("green")

        }
 if indexPath.row == 0 {

        println("red")

        }
}

1 个答案:

答案 0 :(得分:0)

我想出答案......

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {



   var rownumber = indexPath.row

    if   dataToDisplay[rownumber] == sampleData[0] {
   println("red")

    }

}