我的Swift应用程序,我需要在tableview中显示数据。服务器端的数据经常更改,因此我需要删除任何以前的数据并添加提取的新数据。我能够删除和获取,我可以在控制台和领域浏览器中看到新数据。但是,结果中的表视图控制器中没有数据。
class ServiceProvidersTableViewController: UITableViewController {
// MARK: - Data Source
var serviceSubCategory: String = ""
var serviceSubCategories: Results<ServiceSubCategory>?
// MARK: - Realm
let realm = try! Realm()
let fetchData = FetchData()
override func viewDidLoad() {
super.viewDidLoad()
self.deleteSubCategories()
FetchData.get(ServiceSubCategory.self, params: ["subCategoryId": serviceSubCategory], success: {
self.infoAlert("Service Categories", message: "Fetch Successful")
}) {
self.infoAlert("Failed", message: "Fetch Failed")
}
self.fetchSubCategories()
}
func deleteSubCategories()
{
let subCategories = realm.objects(ServiceSubCategory.self)
print("Before delete \(subCategories)")
try! realm.write{
realm.delete(subCategories)
}
print("After delete \(subCategories)")
}
func fetchSubCategories() {
self.realm.refresh()
self.serviceSubCategories = self.realm.objects(ServiceSubCategory)
tableView.reloadData()
print("After fetch \(self.serviceSubCategories)")
}
struct Storyboard {
static let CellIndentifier = "ServiceProvidersCell"
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(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
print(self.serviceSubCategories!.count)
return self.serviceSubCategories!.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellIndentifier, forIndexPath: indexPath) as! ServiceProvidersTableViewCell
print(serviceSubCategories)
cell.serviceSubCategories = self.serviceSubCategories![indexPath.row]
return cell
}
func infoAlert(title:String,message:String) -> Void {
let actionSheetController: UIAlertController = UIAlertController(title:title, message:message, preferredStyle: .Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in }
actionSheetController.addAction(cancelAction)
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
我的猜测是你需要从本地“serviceSubCategory”中删除该对象,而不只是从服务器中删除。
每次服务器侦听器触发时都放置一个tableView.reloadData()
。
可能在fetchSubCategories()
和deleteSubCategories()
函数中。
答案 1 :(得分:0)
我解决了(我应该说,我是愚蠢的)如下: -
y=x
方法有回调FetchData
,在我使用的示例中显示警告。
我在tableViewController中创建了另一个函数,并在success
回调中调用了它。在新方法中,我调用了success
。
感谢所有帮助我的人