简体   繁体   中英

Unable to reload tabledata from UISegmentedControl

I want to show different data in tableview on basis of selection from segmented control. Below is my code it only shows first TABLEVIEW DATASOURCE and for some reason segemented control doesn't work.

@IBOutlet weak var segementedControlAction: UISegmentedControl!


var Names = ["Name1", "Name2","Name3","Name4"]
var Images =  ["1.jpg","2.jpg","5.jpg","8.jpg"]
var set =  ["14","15","16","17"]

var Names1 = ["Name5", "Name6","Name7","Name8"]
var Images1 =  ["6.jpg","4.jpg","7.jpg","9.jpg"]
var set1 =  ["4","5","6","7"]

var Names2 = ["Name9", "Name12","Name13","Name14"]
var Images2 =  ["11.jpg","21.jpg","51.jpg","81.jpg"]
var set2 =  ["114","115","116","117"]


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count: Int = 0
    tableView.layer.cornerRadius = 10
    tableView.layer.masksToBounds = true

    if (tableView == self.d1) {
        print("Monday")
        count = self.Names.count
    }
    else if (tableView == self.d2) {
        print("Tuesday")
        count = self.Names1.count
    }
    else if (tableView == self.d3) {
        print("Wednesday")
        count = self.Names2.count
    }

    return count
}


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

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
        indexPath) as! CustomTableViewCell

if segementedControlAction.selectedSegmentIndex == 0 {
    cell.nameLabel.text = Names[indexPath.row]
    cell.thumbnailImageView.image = UIImage(named: Images[indexPath.row])
    cell.setLabel.text = set[indexPath.row]

    }

else if segementedControlAction.selectedSegmentIndex == 1 {

cell.nameLabel.text = Names1[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: Images1[indexPath.row])
cell.setLabel.text = set1[indexPath.row]
    }


else {segementedControlAction.selectedSegmentIndex == 2

cell.nameLabel.text = Names2[indexPath.row]
cell.thumbnailImageView.image = UIImage(named: Images2[indexPath.row])
cell.setLabel.text = set2[indexPath.row]

    }

    return cell }

@IBAction func segementedControlAction(sender: UISegmentedControl) {
 switch sender.selectedSegmentIndex {

case 0:
self.d1.hidden = false
self.d2.hidden = true
self.d3.hidden = true

self.d1.reloadData()

case 1:

    self.d1.hidden = true
    self.d2.hidden = false
    self.d3.hidden = true
    self.d2.reloadData()

case 2:

    self.d1.hidden = true
    self.d2.hidden = true
    self.d3.hidden = false
    self.d3.reloadData()



default:
    self.d1.hidden = false
    self.d2.hidden = true
    self.d3.hidden = true

    self.d1.reloadData()
    break;

}

try this:

In your viewDidLoad method, add this:

tableView.delegate = self
tableView.dataSource = self

Please ensure that segementedControlAction is connected properly from your view controller. Probably try with these steps:

  1. Delete the earlier connected IBAction .
  2. Connect your segmentedControl to Files Owner.
  3. Connect your segementedControlAction: action and select "Value Changed" NOT "Touch up Inside". THIS IS IMP.
  4. Print a statement to check if segementedControlAction: is now triggered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM