gpt4 book ai didi

swift - 删除、添加和显示数据 swift TableView Controller

转载 作者:行者123 更新时间:2023-11-30 13:16:43 24 4
gpt4 key购买 nike

我是我的 Swift 应用程序,我需要在表格 View 中显示数据。服务器端的数据经常发生变化,因此我需要删除以前的所有数据并添加获取的新数据。我能够删除和获取,并且可以在控制台和 Realm 浏览器中看到新数据。但是,数据在结果的 TableView Controller 中不可用。

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)
}
}

最佳答案

我的猜测是,您需要从本地“serviceSubCategory”中删除该对象,而不仅仅是从服务器中删除。

每次服务器监听器触发时放置一个tableView.reloadData()

可能在 fetchSubCategories()deleteSubCategories() 函数中。

关于swift - 删除、添加和显示数据 swift TableView Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38149614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com