iosswiftuitableview

Is there a way I can reuse a UITableView?


I am new to iOS development and I am using Swift. I have 2 custom table views that have identical layout. The only difference is the data coming back via JSON URL. One table view is called HomePageC and the other UserProfileC.

How can I use what I have in Prototype HomePageC and reuse it in UserProfileC? It is like having a homepage of data then seeing individual users' data, hence userProfile but same layout because I think it's redundant doing 2 table views that are identical.

This is the HomePageC code:

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]
 
   return cell
}

UserProfileC code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "UserProfileTVC", 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 = Votes[indexPath.row]

    return cell
}

I tried to cast it to HomePage but it gives an error.

enter image description here


Solution

  • One possible way to have reusable table is to create a single separated UITableViewController in Storyboard and design your prototype cell there. Instead of having two different reuse identifiers — "HomePageTVC" and "UserProfileTVC" — you can now have a single one (just leave "UserProfileTVC"). Don't forget to set dataSource and delegate of the table view controller to itself.

    Next step: add a container view to any view controller you want to reuse this table in (both HomePageC and UserProfileC controllers in your case).

    Container view

    Now just Control+Drag cursor from the created container views to your reusable table view controller in Storyboard to establish a container relationship.

    Now you have a single UITableViewController class to put all cells management logic to, and you can easily load different data based on current parentViewController property — just check what type of controller your table is currently embedded in:

    if parentViewController is HomePageC {
        // Load user data for Home page controller
    } else if parentViewController is UserProfileC {
        // Load user data for user profile controller
    }