分段控制和表格视图问题迅速

时间:2018-03-22 09:18:27

标签: swift

所以我正在创建一个带有分段控件的表视图。起初 - 我没有进行分段控制,但修改了一些代码。当我向数组添加数字时,它实际上并不填充表视图的单元格。在表格视图中,每次单击向数组附加金额的按钮时,线条都会增加,但不会显示任何数据。所以我知道它正在工作,但实际上并没有在表格视图单元格中显示数字。我的代码不会填充视图的具体原因是什么?

class SpendingHistoryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {


var savedInformation = false
var newDefault : Double?
var selectedSegment = 1
let array2 = ["1","2","3"]


let userDefaults = UserDefaults.standard

@IBOutlet weak var tableViewSpending: UITableView!
@IBOutlet weak var youSpentLabel: UILabel!

@IBOutlet weak var youSavedLabel: UILabel!

@IBOutlet weak var segmentControl: UISegmentedControl!


override func viewDidLoad() {
    super.viewDidLoad()

    self.tableViewSpending.delegate = self
    self.tableViewSpending.dataSource = self

    //GETS user defaults that was set in ADDSUBTRACTMONEY VC - and then sets the default to Array- then gets total
    let moneySpentArray = UserDefaults.standard.array(forKey: "moneySpent") as? [Double] ?? [Double]()
    AddSubtractMoneyController.moneySpentArray = moneySpentArray
    let arraySum = moneySpentArray.reduce(0.0, +)
    youSpentLabel.text = String(format: "%.2f", arraySum)

    }

@IBAction func spentSavedAction(_ sender: UISegmentedControl) {

    if sender.selectedSegmentIndex == 0 {
        selectedSegment = 1

    }
    else {
        selectedSegment = 2
    }

    self.tableViewSpending.reloadData()
}


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

    if selectedSegment == 1 {

    return(AddSubtractMoneyController.moneySpentArray.count)

    }

    else {

        return array2.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    //let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
    //let cell2 = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell2")

    let cell2 = tableViewSpending.dequeueReusableCell(withIdentifier: "cell2")! as UITableViewCell
    let cell = tableViewSpending.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell


    cell.textLabel?.text = String(format: "%.2f", AddSubtractMoneyController.moneySpentArray[indexPath.row])
    cell2.textLabel?.text = array2[indexPath.row]

    self.tableViewSpending.reloadData()

    if selectedSegment == 1 {
        return cell
    }
    else {
        return cell2
    }

        }



}

0 个答案:

没有答案
相关问题