extension DashboardView: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recents.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let recent = recents[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell
cell .setRecent(recent: recent)
cell.delegate = self
return cell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let content = contents[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as! ContentCell
cell .setContent(content: content)
cell.delegate = self
return cell
}
}
我试过创建其他扩展但不起作用
最佳答案
I have tried creating other extensions but does not work
您需要为具有不同布局用途的第一个单元格更改 cellForRowAt
e.x
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let recent = recents[indexPath.row]
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell1") as! RecentCell1
cell .setRecent(recent: recent)
cell.delegate = self
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell2") as! RecentCell2
cell .setRecent(recent: recent)
cell.delegate = self
return cell
}
}
关于ios - 我正在尝试创建多个具有不同类的自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57040584/