简体   繁体   English

无法从UISegmentedControl重新加载表数据

[英]Unable to reload tabledata from UISegmentedControl

I want to show different data in tableview on basis of selection from segmented control. 我想根据分段控件的选择在tableview中显示不同的数据。 Below is my code it only shows first TABLEVIEW DATASOURCE and for some reason segemented control doesn't work. 下面是我的代码,它仅显示第一个TABLEVIEW DATASOURCE,并且由于某些原因,分段控制无法正常工作。

@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: 在您的viewDidLoad方法中,添加以下内容:

tableView.delegate = self
tableView.dataSource = self

Please ensure that segementedControlAction is connected properly from your view controller. 请确保从您的视图控制器正确连接了segementedControlAction Probably try with these steps: 可能尝试执行以下步骤:

  1. Delete the earlier connected IBAction . 删除先前连接的IBAction
  2. Connect your segmentedControl to Files Owner. 将您的segmentedControl连接到文件所有者。
  3. Connect your segementedControlAction: action and select "Value Changed" NOT "Touch up Inside". 连接您的segementedControlAction:操作,然后选择“值已更改”而不是“内部补全”。 THIS IS IMP. 这是IMP。
  4. Print a statement to check if segementedControlAction: is now triggered. 打印一条语句以检查segementedControlAction:现在是否已触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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