Tạo tệp XIB:
Tệp -> Tệp mới -> ios-> lớp cảm ứng ca cao -> tiếp theo

đảm bảo dấu kiểm "cũng tạo tệp XIB"
Tôi muốn biểu diễn với tableview
vì vậy tôi đã chọn lớp phụUITableViewCell
bạn có thể chọn làm yêu cầu của mình

Định hướng tệp XIB theo mong muốn của bạn (RestaurantTableViewCell.xib)

chúng ta cần lấy chiều cao hàng để đặt bảng mỗi hàng hegiht

Hiện nay! cần phải giấu chúng một cách nhanh chóng. tôi đang giấu restaurantPhoto
và restaurantName
bạn có thể giấu tất cả các bạn.

Hiện đang thêm UITableView

tên
Tên của tệp nib, không cần bao gồm phần mở rộng .nib.
chủ sở hữu
Đối tượng để gán làm đối tượng Chủ sở hữu tệp của nib.
tùy chọn
Một từ điển chứa các tùy chọn để sử dụng khi mở tệp nib.
đầu tiên
nếu bạn không xác định trước thì lấy tất cả các chế độ xem .. vì vậy bạn cần lấy một chế độ xem bên trong tập hợp đó frist
.
Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView
đây là bộ điều khiển xem bảng Mã đầy đủ
import UIKit
class RestaurantTableViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let restaurantTableviewCell = Bundle.main.loadNibNamed("RestaurantTableViewCell", owner: self, options: nil)?.first as! RestaurantTableViewCell
restaurantTableviewCell.restaurantPhoto.image = UIImage(named: "image1")
restaurantTableviewCell.restaurantName.text = "KFC Chicken"
return restaurantTableviewCell
}
// set row height
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
}
bạn đã hoàn thành :)
