segementedControlAction

segementedControlAction

我想根据分段控件中的选择在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;

}

最佳答案

请确保从您的视图控制器正确连接segementedControlAction。可能尝试执行以下步骤:


删除先前连接的IBAction
将您的segmentedControl连接到文件所有者。
连接您的segementedControlAction:操作,然后选择“值
已更改”,而不是“在内部进行润饰”。这是IMP。
打印一条语句以检查segementedControlAction:是否现在
触发。

关于ios - 无法从UISegmentedControl重新加载表数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32972831/

10-12 21:20