基于Segment-Control将数组加载到TableView

时间:2015-07-17 11:15:53

标签: ios arrays swift uitableview uisegmentedcontrol

我对Swift比较新。我试图搜索并谷歌问题,但我找不到任何答案。应该不那么难。希望你们能帮助我。我几天来一直在努力解决这个问题:

我创建了一个Tableview,它从另一个.swift文件中加载一组元组。这工作正常!现在我希望tableview根据“段控制”选择.swift。因此,如果Segment-Control切换为“A”,我希望它显示“PSSCBOOKMac.Swift”数组,对于B,它将是“PSSCBOOKWin.swift”的数组。

行动是正确写的,我猜(打印报表正在运作)。但是段控制的改变不会影响Tableview。我的猜测:段控件不会影响Tableview,因为它之前已加载,我无法更改值。我怎样才能做到这一点?

为任何答案干杯!

以下是代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var PSSCSegmentControl: UISegmentedControl!

    //LOAD ARRAY FROM PSSCBOOK.SWIFT
    var PSSCBook = PSSCBOOKMac()

    @IBAction func PSSCSegmentControlChoose(sender: AnyObject) {
        if PSSCSegmentControl.selectedSegmentIndex == 0 {
            var PSSCBook = PSSCBOOKMac()
            println("im mac")
        } else {
            var PSSCBook = PSSCBOOKWin()
            println("im win")
        }
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return PSSCBook.PSSCTools.count
        } else {
            return PSSCBook.PSSCFile.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.PSSCTools[indexPath.row]
            cell.textLabel?.text = shortCutTitle
            cell.detailTextLabel?.text = shortCutKey
        } else {
            let (shortCutTitle,shortCutKey) = PSSCBook.PSSCFile[indexPath.row]
            cell.textLabel?.text = shortCutTitle
            cell.detailTextLabel?.text = shortCutKey
        }

        /*  var PSIcon = UIImage(named: "PSIcon")
        cell.imageView?.image = PSIcon */

        return cell
    }

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

2 个答案:

答案 0 :(得分:2)

当您更新表视图的数据源时,它不会神奇地更新自己。

您必须重新加载表格视图才能进行更改:

@IBAction func PSSCSegmentControlChoose(sender: AnyObject) {
    if PSSCSegmentControl.selectedSegmentIndex == 0 {
        var PSSCBook = PSSCBOOKMac()
        println("im mac")
    } else {
        var PSSCBook = PSSCBOOKWin()
        println("im win")
    }
    self.tableView.reloadData();
}

答案 1 :(得分:0)

我花了最后两天试图找出我的代码出了什么问题。我实现了建议的reloadData()而没有错误。控制台中的println值发生了变化..但是tableview只是不会刷新。我真的不知道在哪里看,被搜索了几个小时。有人可以告诉我,我在做什么类型的愚蠢错误?谢谢你们!

import UIKit

class ViewController: UIViewController, UITableViewDataSource {
//LOAD ARRAY FROM PSSHORTCUTSBOOK.SWIFT

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
    }
  /*  var PSIcon = UIImage(named: "PSIcon")
    cell.imageView?.image = PSIcon */
    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.
}


}