英文:
How do I add the cell I created in different xib to the table view I created in the uiview controller?
问题
final class ViewController: BaseViewController {
private let viewModel = ViewModel()
private let router = Router()
// MARK: - IBOutlets
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var prefenceButton: BorderedButton!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var statusImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
return cell
}
}
class TableViewCell: UITableViewCell, ReusableCell {
static var height: CGFloat = UITableView.automaticDimension
static var estimatedHeight: CGFloat = 150
var router = Router()
var viewModel = ViewModel()
@IBOutlet weak var slipPrefenceLabel: UILabel!
@IBOutlet weak var showAllMerchantLabel: UILabel!
@IBOutlet weak var merchantLabel5: UILabel!
@IBOutlet weak var merchantLabel4: UILabel!
@IBOutlet weak var merchantLabel3: UILabel!
@IBOutlet weak var merchanLabel2: UILabel!
@IBOutlet weak var merchantLabel1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@IBAction func tapShowAllMerchantLabel(_ sender: UITapGestureRecognizer) {
router.routeToSelectedMerchantList()
}
}
英文:
I added tableview to uiview controller, then I created uitableview cell in another xib. I want to show this cell in tableview. But the cell comes empty. I cant create a prototyp cell in xib. The first code blog belongs to vc and the other to the cell I use.
final class ViewController: BaseViewController {
private let viewModel = ViewModel()
private let router = Router()
// MARK: - IBOutlets
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var prefenceButton: BorderedButton!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var statusImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! TableViewCell
return cell
}
}
class TableViewCell: UITableViewCell, ReusableCell {
static var height: CGFloat = UITableView.automaticDimension
static var estimatedHeight: CGFloat = 150
var router = Router()
var viewModel = ViewModel()
@IBOutlet weak var slipPrefenceLabel: UILabel!
@IBOutlet weak var showAllMerchantLabel: UILabel!
@IBOutlet weak var merchantLabel5: UILabel!
@IBOutlet weak var merchantLabel4: UILabel!
@IBOutlet weak var merchantLabel3: UILabel!
@IBOutlet weak var merchanLabel2: UILabel!
@IBOutlet weak var merchantLabel1: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@IBAction func tapShowAllMerchantLabel(_ sender: UITapGestureRecognizer) {
router.routeToSelectedMerchantList()
}
}
答案1
得分: 1
将以下部分翻译为中文:
将此放在视图加载中
self.tableView.register(UINib.init(nibName: "TestTableviewCell1", bundle: nil), forCellReuseIdentifier: "TestTableviewCell1")
放入此代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableviewCell1", for: indexPath) as! TestTableviewCell1
return cell
}
创建自定义单元格类
class TestTableviewCell1: UITableViewCell{
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var checkBtn: UIButton!
}
请注意,XIB(Interface Builder文件)的屏幕截图我无法处理,因此无法提供相关的翻译。
英文:
put this in view did load
self.tableView.register(UINib.init(nibName: "TestTableviewCell1", bundle: nil), forCellReuseIdentifier: "TestTableviewCell1")
put this code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableviewCell1", for: indexPath) as! TestTableviewCell1
return cell
}
create class for custom cell
class TestTableviewCell1: UITableViewCell{
@IBOutlet weak var titleLbl: UILabel!
@IBOutlet weak var checkBtn: UIButton!
}
and create xib I have share screenshot of xib as well.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论