0

I created something similar to mail app.

A tableView (main) which lists database content from API and a search bar on the top.

For a search I bring a view (second) to front which have my search response and I put it in another tableview (second).

For this part it's OK.

Now, I want a navigation to another view but when I select a cell with "pushViewController:" nothing append (I do the same thing in my "main" tableView and it works well).

  • I linked my second table view to the delegate and the datasource.
  • I added "UINavigationControllerDelegate" in .h of my "second" view.

All works well except my second navigation.

Guillaume
  • 393
  • 2
  • 21

2 Answers2

0

You only need one tableView for searching purpose and displaying all data. Just maintain two array. First one contains your original data and second one contain the searched data. As an when search start reload tableView with 2nd array and put all data in it. Like this your navigation will work for both the scenarios.

Swift:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    if isSearch == "true"
    {
        return self.searchArray.count
    }
    else
    {
        return self.arr1.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell:UITableViewCell? =
    tableView.dequeueReusableCellWithIdentifier("tableCell") as? UITableViewCell

    if(cell == nil)
    {
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "tableCell")
        cell!.selectionStyle = UITableViewCellSelectionStyle.None
    }       
    if isSearch == "true"
    {
        var main_string = self.searchArray[indexPath.row]
        var attributedString = NSMutableAttributedString(string:main_string)
            attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor(red: 0.0/255.0, green: 168.0/255.0, blue: 255.0/255.0, alpha: 1.0) , range: NSMakeRange(0, string_to_color.length))
        name.attributedText = attributedString
    }
    else
    {
        name.text = self.arr1[indexPath.row] as String
    }
    return cell!
}
0

Right before the push, check to see if your second navigation controller is null:

  NSLog(@" nav con is %@", mySecondNavigationController);
Rayfleck
  • 12,116
  • 8
  • 48
  • 74
  • Right my second navigation controller is null. Thanks. Do you know how I can link my second navigation controller to the navigation controller of my main view? – Guillaume May 31 '11 at 20:12
  • A nav con is an object like any other; you can pass a reference to it when you initialize your second controller SecondViewController *vc2 = [[SecondViewController alloc] initWithNavigationController:myNavCon]; , or you can make an ivar and have the caller set it directly. vc2.navigationController = myMavCon; – Rayfleck May 31 '11 at 20:45