下拉刷新时 UITableview 出错,所有数据重复出现。任何人都可以帮助使用 Swift3 吗,Xcode8
Error in UITableview when pull down to refresh, all data repeating itself. Can any one help using Swift3 , Xcode8
我的 UITableView
中有来自数据库的数据,当我尝试下拉刷新我的 tableview 中的所有数据时,当我尝试将新数据放入数据库并进行刷新时没有任何反应。
这是我的代码
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
中,有2组相似的对象。
我认为更好的方法是,如果您可以在查询之后附加新数据,或者您可以清空数组,然后从数据库中加载整个数据。
告诉我。
您需要像这样更新您的代码:-
func pullDown() {
var temp = [TestTable]()
for data in mydata {
temp.append(data)
}
mydata.removeAll()
mydata = temp
self.tableView.reloadData()
self.refresh.endRefreshing()
}
我的 UITableView
中有来自数据库的数据,当我尝试下拉刷新我的 tableview 中的所有数据时,当我尝试将新数据放入数据库并进行刷新时没有任何反应。
这是我的代码
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
中,有2组相似的对象。
我认为更好的方法是,如果您可以在查询之后附加新数据,或者您可以清空数组,然后从数据库中加载整个数据。
告诉我。
您需要像这样更新您的代码:-
func pullDown() {
var temp = [TestTable]()
for data in mydata {
temp.append(data)
}
mydata.removeAll()
mydata = temp
self.tableView.reloadData()
self.refresh.endRefreshing()
}