0
votes

How to call method

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
        }
    }

from

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

?

I am beginner of swift.

I am using UISegmentedControl, and UITableView. When I click UISegmentedControl, make it change to UITableViewCell. Please look at inside of func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

I added segconChanged(UISegmentedControl(), text: "") to call method, but I think this is wrong and actually doesnt work.

Please give me advice.

This is all code.

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

    }



}
1

1 Answers

1
votes

You dont need to call segconChanged() function manually as it will automatically be called when segment is changed. So just reload your tableview when segment is changed, check its index and populate data acccordingly i.e. in cellForRowAtIndexPath method.

Try this code :

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

}
}