swift - IOS swift 如何为 Tableview 创建自定义第一个单元格

标签 swift uitableview swift3

我使用的是 swift 3,我有一个自定义的 TableView

但是我试图让第一行与其他行完全不同。这就像 Facebook 应用程序,其中第一行有一个文本框,上面写着“你在想什么?”,其余的 tableView 是相同的。

我的自定义 TableView 如下所示:

enter image description here

我现在想在第一个 TableView 行上放置一个 TextView,我该怎么做呢?

这是我的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "HomePageTVC", for: indexPath) as! HomePageTVC


  cell.post.text = Posts[indexPath.row]
  cell.fullname.setTitle(FullName[indexPath.row],for: UIControlState.normal)
  cell.fullname.tag = indexPath.row
  cell.fullname.addTarget(self, action: #selector(HomePageC.Fullname_Click(sender:)), for: .touchUpInside)
  cell.comments.setTitle(Comments[indexPath.row],for: UIControlState.normal)
  cell.comments.tag = indexPath.row

  cell.votes.setTitle(Votes[indexPath.row],for: UIControlState.normal)      cell.votes.tag = indexPath.row
  cell.time.text = Times[indexPath.row]
  cell.shares.setTitle(Shares[indexPath.row],for: UIControlState.normal)
  cell.shares.tag = indexPath.row
  cell.location.text = Locations[indexPath.row]
  cell.vote_status.text = VoteStatus[indexPath.row]

  return cell
}

最佳答案

您可以创建一个 TableView 标题,该标题将位于该部分的顶部,并且可以不同于您在 cellForRowAtIndexPath 中调用的自定义可重用单元格。

查看此示例,它应该可以帮助您入门。 https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-section-header-to-a-table-view

关于swift - IOS swift 如何为 Tableview 创建自定义第一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42958664/

相关文章:

ios - Swift 3 - 有条件的扩展表格 View 单元格高度

ios - 在 "class func"方法中获取 Swift 类名

iphone - 嵌套 UITableView : a way to scroll both the outer vertical UITableView and a child horizontal UITableView at the same time?

ios - 将 Realm 查询结果用作UITableView节标题

"received memory warning"消息后 iOS UITableView 内容松散

ios - swift 3 google maps - 如何更新 map 上的标记

ios - 单击一个按钮时如何发送事件

swift - 返回箭头作为 Swift 中的类型

ios - 在 iOS 应用程序中打开 Google map View

json - 保存 json 响应并将其解析为 swift 对象