无法从UISegmentedControl重新加载tabledata

时间:2015-10-06 14:41:06

标签: ios swift uitableview swift2 uisegmentedcontrol

我想根据分段控件的选择在tableview中显示不同的数据。下面是我的代码,它只显示第一个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;

}

2 个答案:

答案 0 :(得分:0)

试试这个:

在viewDidLoad方法中,添加:

tableView.delegate = self
tableView.dataSource = self

答案 1 :(得分:0)

请确保从您的视图控制器正确连接segementedControlAction。可能尝试这些步骤:

  1. 删除之前连接的IBAction
  2. 将您的segmentedControl连接到文件所有者。
  3. 连接您的segementedControlAction:操作,然后选择"价值 改变" NOT"触摸内部"。这是IMP。
  4. 打印声明以检查现在是否segementedControlAction: 触发。