Created
July 12, 2024 22:49
-
-
Save Koshimizu-Takehito/f4e1dd90dfa5e9bcb93e7a56e23e83d5 to your computer and use it in GitHub Desktop.
UITableViewPractice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SwiftUI | |
final class ViewController: UIViewController { | |
private var products = Product.products() | |
private lazy var tableView: UITableView = { | |
let tableView = UITableView() | |
tableView.translatesAutoresizingMaskIntoConstraints = false | |
tableView.dataSource = self | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
return tableView | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(tableView) | |
NSLayoutConstraint.activate([ | |
tableView.topAnchor.constraint(equalTo: view.topAnchor), | |
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | |
]) | |
} | |
} | |
extension ViewController: UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
products.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) | |
cell.contentConfiguration = UIHostingConfiguration { | |
CustomCell(product: products[indexPath.row]) | |
} | |
return cell | |
} | |
} | |
struct Product: Identifiable { | |
var id = UUID() | |
var name: String | |
var brand: String | |
var price: Int | |
var icon: String | |
static func products() -> [Product] { | |
[ | |
Product(name: "iPad", brand: "Apple", price: 799, icon: "ipad"), | |
Product(name: "AirPods", brand: "Apple", price: 199, icon: "airpods"), | |
Product(name: "iPhone", brand: "Apple", price: 1299, icon: "iphone"), | |
Product(name: "iMac", brand: "Apple", price: 1999, icon: "display"), | |
Product(name: "Apple Pencil", brand: "Apple", price: 129, icon: "pencil"), | |
Product(name: "MacBook Pro", brand: "Apple", price: 2399, icon: "macbook"), | |
Product(name: "Apple Watch", brand: "Apple", price: 399, icon: "applewatch"), | |
Product(name: "Apple TV", brand: "Apple", price: 179, icon: "appletv"), | |
Product(name: "HomePod", brand: "Apple", price: 299, icon: "homepod"), | |
Product(name: "Mac Mini", brand: "Apple", price: 699, icon: "macmini") | |
] | |
} | |
} | |
struct CustomCell: View { | |
var product: Product | |
var body: some View { | |
HStack(alignment: .top) { | |
Image(systemName: product.icon) | |
.resizable() | |
.aspectRatio(contentMode: .fit) | |
.frame(width: 30, height: 30) | |
.padding() | |
.background( | |
RoundedRectangle(cornerRadius: 8) | |
.stroke(Color.red, lineWidth: 2) | |
) | |
.clipShape(RoundedRectangle(cornerRadius: 8)) | |
.foregroundStyle(.red) | |
VStack(alignment: .leading) { | |
Text(product.name) | |
.font(.headline) | |
Text(product.brand) | |
.font(.subheadline) | |
} | |
.padding(.horizontal) | |
Spacer() | |
VStack(alignment: .leading) { | |
Spacer() | |
Text("$\(product.price)") | |
.font(.subheadline) | |
.foregroundStyle(.secondary) | |
Spacer() | |
} | |
} | |
} | |
} | |
#Preview { | |
ViewController() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment