Swift刷新表视图

时间:2017-11-02 13:35:35

标签: ios swift uitableview

我有一个问题,当我调用viewWillAppear时,表视图得到刷新,但数据很奇怪(我插入一条新记录,所以我每次出现时都需要刷新表视图),例如 原始数据:a,b,c 插入一条记录应该是:a,b,c,d 结果:a,b,c,a(为什么是?) 希望有人可以帮助我。

import UIKit
import SQLite

class DetailViewController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!


let inputDetailTable = Table("input_detail")

var labelOneArray = [String]()
var labelTwoArray = [NSAttributedString]()
var labelThreeArray = [String]()
var labelFourArray = [String]()

let id = Expression<Int>("id")
let deleteFlag = Expression<Int>("delete_flag")

//    var selectedDatas :AnySequence<Row>!



let viewService = ViewService()

override func viewDidLoad() {
    super.viewDidLoad()
    getTableViewData()
}

override func viewWillAppear(_ animated: Bool) {
    getTableViewData()
    self.tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int {
    let selectedDatas = viewService.selectTableData()
    var counter = 0
    for _ in selectedDatas{
        counter += 1
    }
    return counter
}

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


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
for: indexPath) as! DetailTableViewCell

    cell.labelOne.attributedText = labelTwoArray[indexPath.row]
    cell.labelTwo.text = labelFourArray[indexPath.row]

    return cell
}

func getTableViewData() {
    let selectedDatas = viewService.selectTableData()

    for selectedData in selectedDatas{
        var tempAmount = String(selectedData[Expression<Int>
("amount")])
        var tempTwoAmout :NSAttributedString?
        if selectedData[Expression<Int>("type_flag")] == 0 {
            tempAmount = "-"+tempAmount
            let text = tempAmount
            let nsText = text as NSString
            let textRange = NSMakeRange(0, nsText.length)
            let myMutableString = NSMutableAttributedString(
                string: tempAmount,
                attributes: [:])
            myMutableString.addAttribute(
                NSAttributedStringKey.foregroundColor,
                value: UIColor.red,
                range: textRange)
            tempTwoAmout = myMutableString
        } else {
            tempAmount = "+"+tempAmount

            let text = tempAmount
            let nsText = text as NSString
            let textRange = NSMakeRange(0, nsText.length)

            let myMutableString = NSMutableAttributedString(
                string: tempAmount,
                attributes: [:])
            myMutableString.addAttribute(
                NSAttributedStringKey.foregroundColor,
                value: UIColor.green,
                range: textRange)
            tempTwoAmout = myMutableString
        }
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
        let createTimeFormat = formatter.string(from: 
selectedData[Expression<Date>("create_time")])
        self.labelOneArray.append(String(selectedData[Expression<Int>
("id")]))
        self.labelTwoArray.append(tempTwoAmout!)

self.labelThreeArray.append(String(selectedData[Expression<String>
("location")]))
        self.labelFourArray.append(createTimeFormat)
    }
}
}

1 个答案:

答案 0 :(得分:0)

您正在调用两次getTableViewData(),并尝试在获取新数据之前从阵列中删除所有数据。