当我单击 UISegmentedControl 时,将其更改为 UITableViewCell

When I click UISegmentedControl, make it change to UITableViewCell

如何调用方法

internal func segconChanged(segcon: UISegmentedControl, var text:String){

        switch segcon.selectedSegmentIndex {
        case 0:
            print("whoo")
            return text = "clicked Trainings"
        case 1:
            print("yeahh")
            return text = "clicked Daily"
        default:
            break
        }
    }

来自

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

?

我是swift的初学者。

我正在使用 UISegmentedControl 和 UITableView。 当我单击 UISegmentedControl 时,将其更改为 UITableViewCell。 请查看 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

的内部

我添加了 segconChanged(UISegmentedControl(), text: "") 来调用方法, 但我认为这是错误的,实际上是行不通的。

请多多指教

这就是全部代码。

import UIKit

class TrainingLogViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var myTableView: UITableView!
    @IBOutlet weak var segment: UISegmentedControl!
    let tabLog: NSArray = ["Trainings", "Daily"]



    override func viewDidLoad() {
        super.viewDidLoad()
        myTableView.delegate = self
        myTableView.dataSource = self
        segment.addTarget(self, action: "segconChanged:", forControlEvents: UIControlEvents.ValueChanged)

    }

    var text: String = ""

    internal func segconChanged(segcon: UISegmentedControl, var text:String){

        switch segcon.selectedSegmentIndex {
        case 0:
            print("whoo")
            return text = "clicked Trainings"
        case 1:
            print("yeahh")
            return text = "clicked Daily"
        default:
            break
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 10
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let text: String = ""
        *segconChanged(UISegmentedControl(), text: "")
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "LogTraining")
        cell.textLabel?.text = text
        cell.detailTextLabel?.text = "subtitle"
        return cell

    }



}

您不需要手动调用 segconChanged() 函数,因为它会在段更改时自动调用。因此,只需在段更改时重新加载您的表视图,检查其索引并相应地填充数据,即在 cellForRowAtIndexPath 方法中。

试试这个代码:

import UIKit

class TrainingLogViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var segment: UISegmentedControl!
let tabLog: NSArray = ["Trainings", "Daily"]

override func viewDidLoad() {
    super.viewDidLoad()
    myTableView.delegate = self
    myTableView.dataSource = self
    segment.addTarget(self, action: "segconChanged:", forControlEvents: UIControlEvents.ValueChanged)

}

func segconChanged(segcon: UISegmentedControl){
    self.myTableView.reloadData()
    switch segcon.selectedSegmentIndex {
    case 0:
        print("clicked Trainings")
    case 1:
        print("clicked Daily")
    default:
        break
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCellWithIdentifier("LogTraining", forIndexPath: indexPath) 
    cell.textLabel?.text = (self.segment.selectedSegmentIndex == 0) ? tabLog[0] : tabLog[1]
    cell.detailTextLabel?.text = "subtitle"
    return cell

}
}