0

I am working on show weather project. I am listing the cities from database. When I click on a city, another viewcontroller should open. But when I click on a city, the didSelectRowAtIndexPath function does not work.

Here is the my code:

class HavaViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var havatableView: UITableView! override func viewDidLoad() { super.viewDidLoad() self.havatableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier") havatableView.dataSource = self // XMl Parse let url = NSURL(string: "http://haberftp.trt.net.tr/haberservis/havadurumu.aspx") let data = NSData(contentsOfURL: url!) if(data != nil){ let dataxml = NSString(data:data!, encoding: NSUTF8StringEncoding) let xml = SWXMLHash.parse(dataxml!) for merkez in xml["HavaTahmini"]["Merkez"] { var cityName = merkez.element?.attributes["MerkezAdi"] for gun in merkez["Gun"] { // println(gun.element?.attributes["Tarih"]) var tarih = gun.element?.attributes["Tarih"] var endusuk = gun["EnDusuk"].element?.text var enyuksek = gun["EnYuksek"].element?.text var durum = gun["DurumuKod"].element?.text } } } self.loadData() println("hava") // Do any additional setup after loading the view. } func newCityWeather(cityName: String, tarih: String, endusuk: String, enyuksek: String, durum: String){ var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate ) var context: NSManagedObjectContext = appDel.managedObjectContext! var newCity = NSEntityDescription.insertNewObjectForEntityForName("Hava", inManagedObjectContext: context) as NSManagedObject newCity.setValue(cityName, forKey: "sehir") newCity.setValue(tarih, forKey: "tarih") newCity.setValue(endusuk, forKey: "endusuk") newCity.setValue(endusuk, forKey: "enyuksek") newCity.setValue(endusuk, forKey: "endusuk") newCity.setValue(endusuk, forKey: "durum") context.save(nil) // println(newCity) } func newCity(cityName: String){ var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate ) var context: NSManagedObjectContext = appDel.managedObjectContext! var newCity = NSEntityDescription.insertNewObjectForEntityForName("Sehirler", inManagedObjectContext: context) as NSManagedObject newCity.setValue(cityName, forKey: "sehir") context.save(nil) } func loadData() { var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate ) var context: NSManagedObjectContext = appDel.managedObjectContext! var request = NSFetchRequest(entityName: "Sehirler") request.returnsObjectsAsFaults = false var results: NSArray = context.executeFetchRequest(request, error: nil)! if results.count>0 { for res in results { // println(res) } }else { println("sonuc yok") } } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 81 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell : HavaTableViewCell = tableView.dequeueReusableCellWithIdentifier("mycell") as HavaTableViewCell var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate ) var context: NSManagedObjectContext = appDel.managedObjectContext! var request = NSFetchRequest(entityName: "Sehirler") request.returnsObjectsAsFaults = false var results: NSArray = context.executeFetchRequest(request, error: nil)! if results.count>0 { var cities = [String]() for result in results { var res = result as NSManagedObject let city = res.valueForKey("sehir") as String cities.append("\(city)") } cell.setCell(cities[indexPath.row]) } return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { println("click") println(indexPath.row) // performSegueWithIdentifier("showHavaDetay", sender: self) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

3 Answers 3

1

You are missing this:

havatableView.delegate = self 
Sign up to request clarification or add additional context in comments.

Comments

1

I faced the same issue when compared two identical code examples where one was working and the other was not calling didSelectRowAtIndexPath

There are at least two ways to solve the issue:

1) In the code itself:

@IBOutlet weak var table: UITableView! override func viewDidLoad() { table.delegate = self table.dataSource = self //data source might be already set if you see contents of the cells //the main trick is to set delegate } 

2) Using Storyboard or Document Outline (which was the problem in my case cause storyboard changes are not visible in .swift controller classes.

Open Document Outline and Control + Press your TableView you will see two outlets called "delegate" and "dataSource" drag them 1 by 1 to the containing ViewController (right onto the yellow circle)

That's it!

Comments

0

You have to set the delegate property of the UITableView to get the didSelectRowAtIndexPath function to fire. Just declaring the class as supporting the UITableViewDelegate protocol is not enough.

havatableView.delegate = self 

Setting the delegate to self tells the UITableView that the current class has the functions in the UITableViewDelegate

For more information : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/occ/instp/UITableView/delegate

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.