[英]Error in UITableview when pull down to refresh, all data repeating itself. Can any one help using Swift3 , Xcode8
I have data coming from database in my UITableView
, when I try to pull down to make refresh all data in my tableview repeat itself and when I am trying to put a new data in database and making refresh nothing happens. 当我尝试下拉刷新表视图中的所有数据时,我会在UITableView
来自数据库的数据,而当我尝试将新数据放入数据库中并进行刷新时,则没有任何反应。
This is my code 这是我的代码
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()
}
}
As far as i can understand, its because of this code here 据我了解,这是因为这里的代码
for data in mydata {
self.mydata.append(data)
}
Here actually you are iterating the data inside the tableview, and also adding the data already there, and also fetching new data, hence in the actual array self.mydata
, there are 2 sets of similar objects. 实际上,您是在self.mydata
中的数据,还添加了已经存在的数据,还获取了新数据,因此在实际数组self.mydata
,有2组相似的对象。 I think a better approach would be if you can just append the new data after the query or you can empty the array and then load the whole data from your database. 我认为更好的方法是,您可以只在查询后追加新数据,也可以清空数组,然后从数据库中加载整个数据。
Let me know. 让我知道。
You need to update your code like this:- 您需要像这样更新代码:
func pullDown() {
var temp = [TestTable]()
for data in mydata {
temp.append(data)
}
mydata.removeAll()
mydata = temp
self.tableView.reloadData()
self.refresh.endRefreshing()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.