【发布时间】:2018-09-24 07:21:41
【问题描述】:
将数据保存到核心数据上没有问题。 我的问题在于我应该获取数据的那个 vc。
核心数据
@objc(ItemInCard)
public class ItemInCard: NSManagedObject {
}
获取
let fetchRequest : NSFetchRequest<ItemInCard> = ItemInCard.fetchRequest()
do
{
let itemIncard = try PersistanceService.context.fetch(fetchRequest)
self.item = itemIncard
self.Tableview.reloadData()
}
catch
{
}
表格视图
var item = [ItemInCard]()
//MARK: - Table view
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("=======")
print(item)
return item.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ShoppingCardCell") as? ShppingCellCustomTableViewCell
cell!.textLabel?.text = item[indexPath.row].productTitleCore
self.Tableview.reloadData()
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 180
}
只要该 vc 在应用程序中打开,它就会打印核心数据(我添加了打印以检查发生了什么
它不断打印下面的核心数据
[<ItemInCard: 0x281a4efd0> (entity: ItemInCard; id: 0x87c125f9e2c25dff <x-coredata://139AE9B3-30B6-4857-8313-456481AF833C/ItemInCard/p1> ; data: {
paymentTypeCore = "\U0648\U0627\U0631\U064a\U0632\U064a";
priceCore = 10000;
priceTypeCore = "\U0639\U0627\U062f\U06cc-test";
productTitleCore = "\U062c\U0648 \U0627\U06a9\U0631\U0627\U06cc\U0646 \U0628\U0646\U062f \U0627\U0645\U0627\U0645 \U062a\U062d\U0648\U06cc\U0644 \U0645\U0647\U0631\U
但我的应用中只有一个数据。表格也没有填充
【问题讨论】:
-
Offtopic:将 TableView 重命名为 tableView 并将“as?ShppingCellCustomTableViewCell”更改为“as!ShppingCellCustomTableViewCell”
标签: swift uitableview core-data iphonecoredatarecipes