1

Here my code :

    @IBOutlet weak var billSelectorBtn: UIButton!

    @IBOutlet weak var optionListTB: UITableView!
 func numberOfSectionsInTableView(tableView: UITableView!) -> Int {

        return 1

    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as UITableViewCell
        cell.textLabel?.text = optionsListNames[indexPath.row] as? String
         return cell

    }

    private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {


        let selectedItem = optionsListNames.object(at: indexPath.row) as! NSString

        billSelectorBtn.setTitle(selectedItem as String, for: UIControlState.normal)

        if selectedItem .isEqual(to: "Current Bill")
        {

            self.view.backgroundColor = UIColor.red
        }

        else if selectedItem .isEqual(to: "Water Bill")
        {
            self.view.backgroundColor = UIColor.green
        }

            optionListTB.isHidden = true
    } 

So in my didselectrow method i am check whether the selected string is equal.And if its equal i will keep the ui as some color chnage. But its not working ??

i saw this tutorial : http://www.aegisiscblog.com/how-to-implement-custom-dropdown-list-in-swift.html

please help me out..

Thanks

hybrid Dev
  • 529
  • 3
  • 14
  • 33

3 Answers3

1

tableview default color is white that you added in storyboard. To match tableview color to its background view you have to clear the tableview color and vice versa for the Uitableviewcell.

In your viewdidload add this line,

tableview.backgroundColor =     UIColor.clear //tableview is your tableview outlet

And in cellForRowAt indexPath

Add,

  cell.backgroundColor =     UIColor.clear

In swift 3 didselect syntax has been changed to

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    }
Maddy
  • 1,660
  • 11
  • 24
  • i am not getting you.. my did select only not working....you these code in my others function ???? – hybrid Dev May 27 '17 at 20:34
  • see in my did select , if i select any option from table view..it have to display as my button name right ??..that only coded there... but ist not working in my screen...and also no bg color chnaging – hybrid Dev May 27 '17 at 20:36
  • you have to clear tableview color so when you change the `self.view` background color on `didselect` the other color will automatically change to that one as those will be transparent – Maddy May 27 '17 at 20:38
  • just add this code first if u did not get your desired result then we will proceed further – Maddy May 27 '17 at 20:39
  • i did get this work,,,and then only i explained the problem niw – hybrid Dev May 27 '17 at 20:39
  • no , still same thing...if i select any option from my table view...ist not showing as my button title, and table view also not hidden, and no view colr changing...it seems like no entry to my did select fuction – hybrid Dev May 27 '17 at 20:42
  • i tried your solution..but it din work.... My first problem is that did select function is not at all calling...i checked like this ` func tableView(_tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { print("did seelct pressed") }` – hybrid Dev May 27 '17 at 20:49
  • Ok seems the issue is in the method name its not swift 3 function name. Its swift 2. – Kegham K. May 27 '17 at 21:19
  • @Maddy can u help me here https://stackoverflow.com/questions/44222202/ionic-3-app-size-is-more-than-30-mb – hybrid Dev May 27 '17 at 22:42
0

Instead of isEqual, use "==" to compare strings. And make your tableView's backgroundColor and cell's backgroundColor to clear to see the color change, if you have your tableView occupy fullscreen.

badhanganesh
  • 3,427
  • 3
  • 18
  • 39
  • i tried your solution..but it din work.... My first problem is that did select function is not at all calling...i checked like this ` func tableView(_tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { print("did seelct pressed") }` – hybrid Dev May 27 '17 at 20:49
  • Did you make your viewController conform to these protocols: `UITableViewDelegate` and `UITableViewDatasource` ? – badhanganesh May 27 '17 at 20:51
  • can u help me here https://stackoverflow.com/questions/44222202/ionic-3-app-size-is-more-than-30-mb – hybrid Dev May 27 '17 at 22:42
0

Remove private from your delegate method

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
    /// add implementation
} 

See also

Jože Ws
  • 1,754
  • 17
  • 12