Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Jim Mathes
Jim Mathes
2,358 Points

Control TableView with Segment Control (reloadData-Issue)

Hey HappyTreeFriends, I created a Tableview which loads an array of tuples from another .swift file. That is working fine! Now I want the tableview to choose the .swift based on a "segment control". So if the Segment-Control is switched to "A" I want it to show the Array of "PSShortCutsBook.Swift", for B it would be the Array of "PSShortCutsBook2.swift".

The Action ist written properly, I guess (print-statements are working). But the change of the segment-control doesn't effect the Tableview. I added a reloaded the Data but still nothing happens. It won't refresh! Can anyone Tell me what I am doing wrong?

Thanks!

Here is the Code:

import UIKit

class ViewController: UIViewController, UITableViewDataSource {

var PSSCBook = PSShortCutsBook()

@IBOutlet weak var SCOutlet: UISegmentedControl!
@IBOutlet weak var SCtableviewOutlet: UITableView!

@IBAction func SCAction(sender: AnyObject) {
    if SCOutlet.selectedSegmentIndex == 1 {
        var PSSCBook = PSShortCutsBook()
        println("There are \(PSSCBook.shortCutsPS.count) items in this Array")
        self.SCtableviewOutlet.reloadData();

    } else {

        var PSSCBook = PSShortCutsBook2()
        println("There are \(PSSCBook.shortCutsPS.count) items in this Array")
        self.SCtableviewOutlet.reloadData();
    }
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 2
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return PSSCBook.shortCutsPS.count
    } else {
        return PSSCBook.shortCutsPS2.count
    }
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("PSCell", forIndexPath: indexPath) as! UITableViewCell

    if indexPath.section == 0 {
        let (shortCutTitle,shortCutKey) = PSSCBook.shortCutsPS[indexPath.row]
        cell.textLabel?.text = shortCutTitle
        cell.detailTextLabel?.text = shortCutKey

    } else {

        let (shortCutTitle,shortCutKey) = PSSCBook.shortCutsPS2[indexPath.row]
        cell.textLabel?.text = shortCutTitle
        cell.detailTextLabel?.text = shortCutKey
    }
    return cell
}

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Tools"
    } else {
        return "Help"
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

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

1 Answer

AR Ehsan
AR Ehsan
7,912 Points

I have no idea!