我目前正在编写一个小应用程序,它涉及一个tableView和一个ManagedObjects数组,用于持久存储。
我想要做的是通过单击另一个视图控制器中的按钮来删除阵列中的所有ManagedObjects。
为此,我尝试将数组设为静态变量,遗憾的是,这与我用来使用此数组中的数据填充表的方法冲突。令人沮丧的东西。
以下是该类的代码:
class ClassOverviewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
static var subjects = [NSManagedObject]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return subjects.count
}
static func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell")
let subject = subjects[indexPath.row]
cell!.textLabel!.text = subject.valueForKey("subjectName") as? String
return cell!
}
static func clearSubjects() {
for item in (self.subjects)
{
CalculateClass.managedContext.deleteObject(item)
}
do {
try CalculateClass.managedContext.save()
}
catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}
我已经从班级删除了我认为没必要向你展示的功能。
我不喜欢将第二个tableView方法设为静态,因为我从UITableViewDataSource获取了该方法。
我不确定我该怎么办。请帮忙!
答案 0 :(得分:1)
返回非静态实现,以便表格正常工作。
如果要删除对象:
ClassOverviewController
对象的引用并调用其方法ClassOverviewController
对象它应该重置其数组。