Rome Torres Rome Torres -4 years ago 165
Swift Question

IOS swift how can I create a custom first cell for a Tableview

I am using swift 3 and I have a custom TableView however I am trying to make the first row completely different from all the others . It's like the Facebook app where the first row has a TextBox that states "What's on your mind ? " and the rest of the tableView is the same . My custom TableView looks like this, I now want to put a TextView on the first TableView row, how can I go about doing that? as I am new to IOS development this is my code

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

enter image description here

Answer Source

You can create a table view header which will be at the top of the section and can be different from the custom reuseable cells you call in your cellForRowAtIndexPath.

Check out this example it should get you started.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download