UITableview在下拉刷新时发生错误,所有数据重复。任何人可以帮助使用Swift3,Xcode8
我有我的数据库来自我的UITableView
数据库,当我试图拉下来刷新我的tableview中的所有数据重复自己,当我试图把一个新的数据在数据库和制作刷新没有发生。UITableview在下拉刷新时发生错误,所有数据重复。任何人可以帮助使用Swift3,Xcode8
这是我的代码
class MyTable: UITableViewController {
var refresh = UIRefreshControl()
@IBAction func SharaOn(_ sender: UIButton) {
}
var mydata = [TestTable]()
let backendless = Backendless()
override func viewDidLoad() {
super.viewDidLoad()
refresh = UIRefreshControl()
refresh.addTarget(self, action: #selector (MyTable.pullDown), for: .valueChanged)
tableView.addSubview(refresh)
}
override func viewDidAppear(_ animated: Bool) {
loaddatawithquery()
}
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return mydata.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "mycell") as? MyTableCell{
let ImgURL = URL(string : self.mydata[(indexPath as NSIndexPath).row].ImgURL!)
let Desc = self.mydata[(indexPath as NSIndexPath).row].Desc!
cell.MyText.text = Desc
cell.mainImage.sd_setImage(with: ImgURL)
return cell
}else{
let cell = MyTableCell()
let ImgURL = URL(string : self.mydata [(indexPath as NSIndexPath).row].ImgURL)
let Desc = self.mydata[(indexPath as NSIndexPath).row].Desc
cell.mainImage.sd_setImage(with: ImgURL)
cell.MyText.text = Desc!
return cell
}
}
func updateLikes() {
let LikeBu = TestTable()
let updaterecord = Backendless.sharedInstance().data.of(TestTable.ofClass())
LikeBu.LikeNo = 1
updaterecord?.save(LikeBu, response: { (result) in
print("udateed successfully \(result)")
}, error: { (Fault) in
print("EROrrrrrrrrrr")
})
}
func loaddatawithquery(){
_ = "ImgURL"
_ = "Desc"
let dataQuery = BackendlessDataQuery()
dataQuery.queryOptions.pageSize=50
// dataQuery.whereClause = whereClause
backendless.data.of(TestTable.ofClass()).find(dataQuery,response: {(result: BackendlessCollection?) -> Void in
let data = result?.getCurrentPage()
for obj in data! as! [TestTable] {
self.mydata.append(obj)
//print("&&&&&&&hhhjjjhhvkhkh \(obj.Desc!) &&&&&&&&&&&&&&&&&&")
self.tableView.reloadData()
}
},
error: { (fault: Fault?) -> Void in
let alert = UIAlertController(title: "info", message:"يرجى الاتصال بالانترنيت", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in
})
self.present(alert, animated: true){}
})
}
@IBAction func likebutton(_ sender: AnyObject) {
// updateLikes()
}
func pullDown() {
for data in mydata {
self.mydata.append(data)
}
self.refresh.endRefreshing()
}
}
至于我可以理解,其因为这段代码在这里
for data in mydata {
self.mydata.append(data)
}
这里其实你迭代的数据的tableview内,并且还加入了数据已经存在,并且还获取新数据,因此在实际数组self.mydata
中,有两组相似的对象。 我认为更好的方法是,如果您可以在查询后追加新数据,或者可以清空数组,然后从数据库加载整个数据。
让我知道。
你需要更新你这样的代码: -
func pullDown() {
var temp = [TestTable]()
for data in mydata {
temp.append(data)
}
mydata.removeAll()
mydata = temp
self.tableView.reloadData()
self.refresh.endRefreshing()
}
我这样做,但仍然是相同的错误添加新的数据和下拉时没有发生。 –
我已经编辑了我的答案,假设您在调用pullDown之前在mydata对象中有新数据。 –
仍然是一样的错误没有发生。 –
我尝试这样做,但是当我在数据库中添加新的数据并拉下没有事情发生。 –
您正在使用的代码现在将追加新数据和旧数据,因此,有两组旧数据,因此您会在数组中获得重复数据。 –
你们是否要删除下拉的func,而不是使用loaddatawithquery的func?如果是这样,我试图做到这一点,但仍然是同样的错误。 –