ios - 我正在尝试创建多个具有不同类的自定义单元格

标签 ios swift iphone uitableview mobile

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/

相关文章:

ios - admob 广告被关闭后执行 segue

ios - 用户在苹果应用商店找不到我们的应用

ios - pickerview 不显示数组中的数据

IOS : Custom Layouts does not work, CollectionView:cellForItemAtIndexPath: 未调用方法

ios - 从详细 View Controller 返回时,侧边栏菜单不起作用

iphone - 如何给图像赋值

iphone - 关闭 iOS 上下文菜单

ios - swift ,iOS 8+ 框架

iOS 重定向到新的 ViewCONtroller 不会加载所有 UI 元素

ios - 在 Swift 上的哪里存储 API key ?