{"id":6806,"date":"2025-05-30T13:06:33","date_gmt":"2025-06-05T05:30:33","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6806"},"modified":"2025-06-05T13:06:33","modified_gmt":"2025-06-05T05:30:33","slug":"https-badgameshow-com-steven-79","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-79\/","title":{"rendered":"Swift UITableView \u8207 Core Data \u5b8c\u6574\u6559\u5b78\uff1a\u5be6\u73fe\u8cc7\u6599\u5132\u5b58\u8207\u8b80\u53d6\u7684\u6700\u4f73\u5be6\u8e10 \ud83d\udcbe\ud83d\udcc3"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, UITableView, Core Data, iOS \u6559\u5b78, \u8cc7\u6599\u7ba1\u7406\"><\/p>\n<p>\u5728 iOS \u958b\u767c\u4e2d\uff0c\u4f7f\u7528 Swift \u7d50\u5408 UITableView \u8207 Core Data \u4f86\u5be6\u73fe\u8cc7\u6599\u5132\u5b58\u8207\u8b80\u53d6\u662f\u4e00\u500b\u975e\u5e38\u666e\u904d\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u63d0\u4f9b **2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10**\uff0c\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 Swift \u5b8c\u6210\u9019\u500b\u529f\u80fd\uff0c\u4e26\u9644\u4e0a\u5be6\u4f5c\u7bc4\u4f8b\u8207\u932f\u8aa4\u6392\u9664\u6280\u5de7\u3002<\/p>\n<h2>\u524d\u7f6e\u6e96\u5099<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728 Xcode \u4e2d\u5efa\u7acb\u4e00\u500b\u65b0\u7684\u5c08\u6848\uff0c\u4e26\u4e14\u5728\u5c08\u6848\u4e2d\u52a0\u5165 Core Data \u652f\u63f4\u3002\u4f60\u53ef\u4ee5\u53c3\u8003<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.apple.com\/documentation\/coredata\/adding_a_core_data_store_to_your_app\" target=\"_blank\" rel=\"noopener\">\u5b98\u65b9\u6587\u4ef6<\/a>\u4f86\u5b8c\u6210\u9019\u500b\u6b65\u9a5f\u3002\u78ba\u4fdd\u4f60\u5728\u5c08\u6848\u8a2d\u5b9a\u4e2d\u555f\u7528 Core Data\u3002<\/p>\n<h2>\u5efa\u7acb UITableView<\/h2>\n<p>\u63a5\u8457\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b UITableView \u4f86\u986f\u793a\u8cc7\u6599\u3002\u8acb\u53c3\u8003<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.apple.com\/documentation\/uikit\/views_and_controls\/table_views\" target=\"_blank\" rel=\"noopener\">\u5b98\u65b9\u6587\u4ef6<\/a>\u4ee5\u7372\u5f97\u66f4\u591a\u8a73\u7d30\u8cc7\u8a0a\u3002\u4ee5\u4e0b\u662f UITableView \u7684\u57fa\u672c\u5efa\u7acb\u6d41\u7a0b\uff1a<\/p>\n<p>1. \u5728\u6545\u4e8b\u677f\u4e2d\u62d6\u66f3 UITableView \u5230\u4f60\u7684\u8996\u5716\u63a7\u5236\u5668\u3002<br \/>\n2. \u8a2d\u5b9a UITableView \u7684 delegate \u548c dataSource\u3002<br \/>\n3. \u5efa\u7acb\u81ea\u5b9a\u7fa9\u7684 UITableViewCell \u4ee5\u986f\u793a\u8cc7\u6599\u3002<\/p>\n<h2>\u5efa\u7acb Core Data Entity<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5efa\u7acb\u4e00\u500b Core Data Entity \u4f86\u5132\u5b58\u8cc7\u6599\u3002\u8acb\u53c3\u8003<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/developer.apple.com\/documentation\/coredata\/modeling_data\/creating_a_data_model\" target=\"_blank\" rel=\"noopener\">\u5b98\u65b9\u6587\u4ef6<\/a>\u4ee5\u4e86\u89e3\u5982\u4f55\u5efa\u7acb\u6a21\u578b\u3002\u8acb\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<p>1. \u6253\u958b .xcdatamodeld \u6a94\u6848\u3002<br \/>\n2. \u65b0\u589e\u4e00\u500b Entity\uff0c\u4e26\u547d\u540d\u70ba &#8220;Entity&#8221;\u3002<br \/>\n3. \u70ba\u6b64 Entity \u6dfb\u52a0\u5c6c\u6027\uff0c\u4f8b\u5982 &#8220;data&#8221;\uff08\u8cc7\u6599\u578b\u614b\u70ba Binary Data\uff09\u3002<\/p>\n<h2>\u5be6\u73fe\u8cc7\u6599\u5132\u5b58\u8207\u8b80\u53d6<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 Swift \u5be6\u73fe\u8cc7\u6599\u5132\u5b58\u8207\u8b80\u53d6\u7684\u5b8c\u6574\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0c\u9069\u7528\u65bc iOS 15 \u53ca\u4ee5\u4e0a\u7248\u672c\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nimport CoreData<\/p>\n<p>\/\/ \u5132\u5b58\u8cc7\u6599<br \/>\nfunc saveData(data: Data) {<br \/>\n    let context = persistentContainer.viewContext<br \/>\n    let entity = NSEntityDescription.entity(forEntityName: &#8220;Entity&#8221;, in: context)!<br \/>\n    let newData = NSManagedObject(entity: entity, insertInto: context)<br \/>\n    newData.setValue(data, forKey: &#8220;data&#8221;)<\/p>\n<p>    do {<br \/>\n        try context.save()<br \/>\n        print(&#8220;Data saved successfully&#8221;)<br \/>\n    } catch {<br \/>\n        print(&#8220;Failed saving: \\(error.localizedDescription)&#8221;)<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u8b80\u53d6\u8cc7\u6599<br \/>\nfunc loadData() -> [Data] {<br \/>\n    let context = persistentContainer.viewContext<br \/>\n    let request = NSFetchRequest<NSFetchRequestResult>(entityName: &#8220;Entity&#8221;)<br \/>\n    request.returnsObjectsAsFaults = false<\/p>\n<p>    do {<br \/>\n        let result = try context.fetch(request)<br \/>\n        var dataList = [Data]()<br \/>\n        for data in result as! [NSManagedObject] {<br \/>\n            if let data = data.value(forKey: &#8220;data&#8221;) as? Data {<br \/>\n                dataList.append(data)<br \/>\n            }<br \/>\n        }<br \/>\n        return dataList<br \/>\n    } catch {<br \/>\n        print(&#8220;Failed loading: \\(error.localizedDescription)&#8221;)<br \/>\n        return []\n    }<br \/>\n}<br \/>\n&#8220;`<br \/>\n\u5728\u4e0a\u8ff0\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 `NSManagedObject` \u4f86\u8655\u7406 Core Data \u7684\u5132\u5b58\u8207\u8b80\u53d6\u3002\u78ba\u4fdd\u5728\u932f\u8aa4\u8655\u7406\u4e2d\u52a0\u5165\u9069\u7576\u7684\u932f\u8aa4\u4fe1\u606f\u4ee5\u65b9\u4fbf\u9664\u932f\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u8a73\u7d30\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Swift \u7d50\u5408 UITableView \u8207 Core Data \u5be6\u73fe\u8cc7\u6599\u5132\u5b58\u8207\u8b80\u53d6\u7684\u529f\u80fd\u3002\u9019\u9805\u6280\u80fd\u5c07\u5927\u5e45\u63d0\u9ad8\u958b\u767c\u8005\u5c0d\u8cc7\u6599\u7684\u7ba1\u7406\u6548\u7387\uff0c\u4e26\u4e14\u5e6b\u52a9\u4f60\u5728 iOS \u61c9\u7528\u4e2d\u66f4\u597d\u5730\u8655\u7406\u8cc7\u6599\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p><strong>Q1: Core Data \u7684\u4f7f\u7528\u5834\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\nA1: Core Data \u9069\u5408\u7528\u65bc\u9700\u8981\u6301\u4e45\u5316\u8cc7\u6599\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c\u5c24\u5176\u662f\u7576\u8cc7\u6599\u7d50\u69cb\u8907\u96dc\u6216\u9700\u8981\u9032\u884c\u67e5\u8a62\u6642\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u8655\u7406 Core Data \u7684\u7248\u672c\u8b8a\u66f4\uff1f<\/strong><br \/>\nA2: \u7576\u6a21\u578b\u9700\u8981\u8b8a\u66f4\u6642\uff0c\u61c9\u4f7f\u7528\u8f15\u91cf\u7d1a\u9077\u79fb\uff0c\u4e26\u5728 `.xcdatamodeld` \u6a94\u6848\u4e2d\u9032\u884c\u7248\u672c\u7ba1\u7406\u3002<\/p>\n<p><strong>Q3: UITableView \u5982\u4f55\u66f4\u65b0\u986f\u793a\u7684\u8cc7\u6599\uff1f<\/strong><br \/>\nA3: \u7576\u8cc7\u6599\u6539\u8b8a\u5f8c\uff0c\u61c9\u547c\u53eb `tableView.reloadData()` \u4f86\u66f4\u65b0 UITableView \u7684\u986f\u793a\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView-\u7d50\u5408-Core-Data-\u5be6\u73fe-\ud83d\udcbe\ud83d\udcc3_1675396061.9341178.webp\" alt=\"Swift UITableView \u7d50\u5408 Core Data \u5be6\u73fe \ud83d\udcbe\ud83d\udcc3\" title=\"Swift UITableView\u3001Core Data\u3001\u8cc7\u6599\u5132\u5b58\u3001iOS\u88dd\u7f6e\u3001\u8cc7\u6599\u7ba1\u7406\" width=\"1200\" height=\"628\"\/><br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528SwiftUITableView\u8207CoreData\u4f86\u5be6\u73fe\u8cc7\u6599\u5132\u5b58\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u5c07\u8cc7\u6599\u5132\u5b58\u5728iOS\u88dd\u7f6e\u4e0a\uff0c\u4e26\u66f4\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6599\u3002<\/p>\n","protected":false},"author":1,"featured_media":6805,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6806","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-UITableView-\u7d50\u5408-Core-Data-\u5be6\u73fe-\ud83d\udcbe\ud83d\udcc3_1675396061.9341178.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1LM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=6806"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6806\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6805"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}