1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- //
- // HomeViewController.swift
- // AIPaint
- //
- // Created by Fengyu He on 2022/11/29.
- //
- import UIKit
- import SnapKit
- import CoreData
- class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
-
- lazy var paintingTableView: UITableView = {
- let tableView = UITableView()
- tableView.separatorStyle = .singleLine
- tableView.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
- tableView.allowsSelection = true
- tableView.allowsMultipleSelection = false
- return tableView
- }()
- var delegate: AppDelegate?
- var context: NSManagedObjectContext?
- var fetchPainting: NSFetchRequest<Painting>?
- var info: [Painting] = []
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- if UITraitCollection.current.userInterfaceStyle == .dark {
- view.backgroundColor = .black
- } else {
- view.backgroundColor = .white
- }
- self.title = "作品"
-
- delegate = UIApplication.shared.delegate as? AppDelegate
- context = delegate!.persistentContainer.viewContext
- fetchPainting = NSFetchRequest<Painting>.init(entityName: "Painting")
-
- paintingTableView.delegate = self
- paintingTableView.dataSource = self
- view.addSubview(paintingTableView)
-
- paintingTableView.snp.makeConstraints { (make) in
- make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
- make.width.equalTo(view.safeAreaLayoutGuide.snp.width)
- make.height.equalTo(view.safeAreaLayoutGuide.snp.height)
- make.centerX.equalTo(view.safeAreaLayoutGuide.snp.centerX)
- }
- }
-
- override func viewWillAppear(_ animated: Bool) {
- do {
- let paintings = try context?.fetch(fetchPainting!)
- for painting in paintings! {
- if !info.contains(painting) {
- info.append(painting)
- } else {
- }
- }
- } catch {}
- paintingTableView.reloadData()
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- print("info loaded \(info.count)")
- return info.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = PaintingCell(style: .default, reuseIdentifier: "PaintingCell")
- cell.setValueForCell(painting: info[indexPath.row])
- return cell
- }
-
-
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 100
- }
- }
|