我使用的是 swift 3,我有一个自定义的 TableView
。
但是我试图让第一行与其他行完全不同。这就像 Facebook 应用程序,其中第一行有一个文本框,上面写着“你在想什么?”,其余的 tableView 是相同的。
我的自定义 TableView 如下所示:
我现在想在第一个 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/