0

I have embedded UITabbar in UIViewController and then I put some UITabbarItems in my UITabbar. I also created another UIViewController with UITableView embedded in it.

I have linked one of the UITabbarItem with this newly created UIViewController(with UITableView in it).

When I run the program and tap on that UITabbarItem it shows a table view but when I select any row of table, the data displaying in UITableView disappears. I then debug the code and found that didSelectRowAtIndexPath method didn't call.

I searched on the internet and find this but cannot understand it well. I tried by implementing both types of tableViews (UITableViewController,UIViewController with UITableView in it) but still same problem.
I am stuck here. Anyone please help me. How to get resolve this issue.
I will be very Thankful to you.
Here is my swift code

import UIKit

class DashboardViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!
    var selectedIndexPathForCheckMark:NSIndexPath?

        var items: [String] = ["We", "Heart", "Swift"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("MyCell")!UITableViewCell

        cell.textLabel?.text = self.items[indexPath.row]
        return cell
    }
 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")

        selectedIndexPathForCheckMark = indexPath
        tableView.reloadData()
    }

here are its snapshots

enter image description here enter image description here Second UITabbarItem from right implements UITableView

Community
  • 1
  • 1
user3314286
  • 243
  • 3
  • 18

1 Answers1

0

Follow Steps:
1. Take A view controller and add tableview.
2. set outlet with MytabelView
3. set Datasource and delegate of tableview to ViewController
4. and put the following code in ViewController class
5. Take a custom tableview cell with name MyCell having cell identifier "MyCellid"
6. Create swift file with name MyCell, make it subclass of UITableViewCell
7. Choose custom cell and assign it class to MyCell.


I think Its done

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var MytableView: UITableView!
 var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.MytableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCellid")
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.MytableView.dequeueReusableCellWithIdentifier("MyCellid")! as UITableViewCell

    cell.textLabel?.text = self.items[indexPath.row]
    return cell
}

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

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("You selected cell #\(indexPath.row)!")

}
}
Avinash Jadhav
  • 491
  • 4
  • 17
  • Dear -Avinash Jadhav I already have set it from storyboard. I also tried it from code as you did but still not resolved. – user3314286 Feb 29 '16 at 13:35
  • Hey, Have you given auto-layout constraints? If not, please check view hierarchy before and after tapping tableview cell. It help to debug whether your view is misplaced on cell selection... – Avinash Jadhav Feb 29 '16 at 13:42
  • recently I checked the view hierarchy in this it shows tableview with its data before I select any row but when I select row, in view hierarchy every thing becomes clean only table view remains there. – user3314286 Feb 29 '16 at 14:09
  • Is print statement printed? What does `selectedIndexPathForCheckMark = indexPath` this code. What is logic for didSelectRow? – Avinash Jadhav Feb 29 '16 at 14:13
  • No print statement doesn't print. This line will be used in my next step logic. But for now I Just want when I select any row in **UITableView** my program should display its **indexparth.row** number. sorry by mistake this line also posted. – user3314286 Feb 29 '16 at 14:23
  • Better way delete that tableView and re add it. [Visit link and go through all solutions it may help you](http://stackoverflow.com/questions/255927/didselectrowatindexpath-not-being-called) – Avinash Jadhav Feb 29 '16 at 14:51
  • can you please write / or provide me with some example code to get this type of functionality as i told you Thank you. I also visited the link provided by you but no help – user3314286 Feb 29 '16 at 15:23
  • Hey Hi, I am posting code for ViewController having tableview. Its same as your code. It successfully logs didSelectRowForIndex path – Avinash Jadhav Feb 29 '16 at 16:18
  • Hi, its still same problem, actually what my requirement is (as I already discuss with you) that I need to have a **UITableView** when I select any `UITabbarItem` in my custom `UITabbar`(**NOT** using `UITabbarController`), I implemented this but `didSelectRowAtIndexPath indexPath` didn't call. I'm very sad because its not working :( – user3314286 Mar 01 '16 at 05:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104957/discussion-between-user3314286-and-avinash-jadhav). – user3314286 Mar 01 '16 at 06:03