滚动时,UITableView单元格重复

时间:2018-08-01 15:42:53

标签: swift uitableview scroll

  

有人可以帮助我吗?我的UITableView单元格在   正在上下滚动,数组结果在indexPath部分上的打印   滚动时始终会重新加载相同的数据

Image result

  

示例代码:

class DetilMutasiRekController: UIViewController, UITableViewDelegate, 
UITableViewDataSource {
  

开始加载视图

var Tgl1: String = ""

var Tgl2: String = ""

var Norek: String!

var PIN: String!

var returnValue: Bool = false

@IBOutlet weak var tableView: UITableView!

var tgl = [String]()
var kode_trans = [String]()
var ket_trans = [String]()
var nominal = [String]()
var trans = [Int]()

let cellReuseIdentifier = "cell"

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.setNavigationBarHidden(false, animated: false)
    self.navigationController?.viewControllers.remove(at: 3)
    MainDashboard(viewController: self, Text: "Mutasi Tabungan")
    if(returnValue == true) {
        loadMutasi()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
        tableView.delegate = self
        tableView.dataSource = self
    }
}
  

启动表格视图

func numberOfSections(in tableView: UITableView) -> Int {
    return self.tgl.count
}

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 115
}
  

// 使用字符串数组填充单元格

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

    let cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)!

    cell.selectionStyle = .none
    cell.FieldListView(Field: self.tgl[indexPath.section], top: 0, bold: true)
    cell.FieldListView(Field: self.kode_trans[indexPath.section], top: 35, bold: false)
    cell.FieldListView(Field: self.ket_trans[indexPath.section], top: 55, bold: false)
    cell.FieldListView(Field: self.nominal[indexPath.section], top: 75, bold: true)
    cell.ArtosListView(color: self.trans[indexPath.section], heightLeft: 115)
    return cell
}
  

// 使用alamofire加载数据

func loadMutasi() {

    let headers: HTTPHeaders = [
        "Content-Type": "application/json",
        "Authorization": "Bearer "+token!
    ]

    let parameters = [
        "from": Tgl1, "norek": Norek ?? "", "pin": PIN ?? "", "to": Tgl2
    ]

    IJProgressView.shared.showProgressView(view)
    Alamofire.request(DataStructure.routes["mutasi"]!, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON {
        response in
        IJProgressView.shared.hideProgressView()
        if let result = response.result.value {
            let swiftyJsonVar = JSON(result)
            let results = swiftyJsonVar["data"].arrayValue
            self.tgl.removeAll()
            for result in results {
                self.tgl.append(result["tgltrn"].string!)
                self.kode_trans.append(result["noref"].string!)
                self.ket_trans.append(result["keterangan"].string!)
                self.nominal.append(result["nilai"].string!)
                if result["doc"] == "C" {
                    self.trans.append(3)
                }
                else {
                    self.trans.append(2)
                }
            }
            if self.tgl.count > 0 {
                self.tableView.reloadData()
            }
        }
    }

 }
}

0 个答案:

没有答案