Hunter
Hunter

Reputation: 421

tableview: search bar that uses indexPath.row

I have researched how to make a search bar in a table view, but they all make their data for the tableview different so when I try to use their method on my tableview, the titles, details, and pictures are misplaced. How would I make a search bar from my code?

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CLLocationManagerDelegate {

var cellContent = ["not actual data for privacy reasons, there are 23 strings in here"]

var cellDetail = ["not actual data for privacy reasons, there are 23 strings in here"]

var cellImage = ["not actual data for privacy reasons, there are 23 names of images in here"]

let locationManager = CLLocationManager()

@IBOutlet weak var tableview: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.requestAlwaysAuthorization()

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

}

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

    if indexPath.row == 0 {

        self.performSegueWithIdentifier("0", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 1 {

        self.performSegueWithIdentifier("1", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 2 {

        self.performSegueWithIdentifier("2", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 3 {

        self.performSegueWithIdentifier("3", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 4 {

        self.performSegueWithIdentifier("4", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 5 {

        self.performSegueWithIdentifier("5", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 6 {

        self.performSegueWithIdentifier("6", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 7 {

        self.performSegueWithIdentifier("7", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 8 {

        self.performSegueWithIdentifier("8", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 9 {

        self.performSegueWithIdentifier("9", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 10 {

        self.performSegueWithIdentifier("10", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 11 {

        self.performSegueWithIdentifier("11", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 12 {

        self.performSegueWithIdentifier("12", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 13 {

        self.performSegueWithIdentifier("13", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 14 {

        self.performSegueWithIdentifier("14", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 15 {

        self.performSegueWithIdentifier("15", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 16 {

        self.performSegueWithIdentifier("16", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 17 {

        self.performSegueWithIdentifier("17", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 18 {

        self.performSegueWithIdentifier("18", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 19 {

        self.performSegueWithIdentifier("19", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 20 {

        self.performSegueWithIdentifier("20", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 21 {

        self.performSegueWithIdentifier("21", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    } else if indexPath.row == 22 {

        self.performSegueWithIdentifier("22", sender: self)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

    }

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section:Int) -> Int {

    return 23
}

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

    let cell: UITableViewCell = (tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell!)
    if (cell == cell) {

    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")

    var imageName = UIImage(named: cellImage[indexPath.row])
    //let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
    //let cell2 = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")

    //let art = cellContent[indexPath.row]
    cell.textLabel?.text = cellContent[indexPath.row]
    cell.detailTextLabel?.text = self.cellDetail[indexPath.row]
    cell.imageView?.image = imageName
    //cell.backgroundColor = UIColor.clearColor()
    //cell.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.1)

        return cell
    }
        return cell

}


  }

Upvotes: 0

Views: 110

Answers (2)

Joe Benton
Joe Benton

Reputation: 3753

Just as a side note to tidy up your code. Rather than doing an if statement for each indexPath on didSelect you can write it once turning the indexPath row into a string and using that as the segue identifier:

self.performSegueWithIdentifier(String(indexPath.row), sender: self)
tableView.deselectRowAtIndexPath(indexPath, animated: true)

Upvotes: 0

pbush25
pbush25

Reputation: 5248

In your cellForRowAtIndexPath: you have to check if the TableView is in search mode or regular mode. And then you can have the TableView dequeue the appropriate cell from the appropriate data source depending on which state the table is in. Best done with an if...else

Upvotes: 0

Related Questions