mharris7190
mharris7190

Reputation: 1374

UITableView with: Static Cells with dynamic content and dynamic cells

I have a UITableView that always has 4 sections. The first section always has 1 row, the second section always has 3 rows. Then the 3rd and 4th section can have any number of rows. Although the first two sections are essentially static cells, the content in them changes.

The first two sections are all I had to begin with so I just used static cells with outlets to the content directly in the TableViewController. Then I added two more sections which can have any number of rows.

I found that to do this, now I have to consider the whole table as using dynamic prototypes and I have to implement all the delegate/datasource classes for every cell now. In addition to that, I can no longer have my outlets (even for the first 2 sections) directly in the tableViewController but instead create custom subclasses for them so I can change their content.

This seems silly because they are so simple. Is there a better way I can do this? Maybe I can have two tableViews in the TVC, one that is static and handled the same way for the static cells and one that uses dynamic prototypes? If that's the best way, how should I implement that? I have never implemented a TableViewController before that handles more than 1 TableView.

For reference, the View is a profile view with simple data about the person's profile. The first 2 sections are just the person's picture, name, email, and description.

The second 2 sections contain 1) a list of favorited postings on the app. And 2) a list of postings that the user has created.

Here are 2 pictures showing the view:

profile view top profile view bottom

Upvotes: 1

Views: 3932

Answers (2)

Mark McCorkle
Mark McCorkle

Reputation: 9414

No, fortunately Apple did add static table cell for UITableViewController but the road ends there. If you want to start modifying content you need to switch to a "real" UITableView, setup arrays and handle the cells correctly.

2 options.

Create IBOutlets to the static cells content and change the objects directly.

or...

Setup the tableView delegate methods and change content using the UITableView delegate and datasource.

class CustomCell: UITableViewCell {
    @IBOutlet weak var labelName: UILabel!
}

In reference to the comment about getting the indexPath of a button click from the viewController check out the following code. Basically I created an extension of UITableView that includes the indexPathForView function. This way you can get the exact view(button) you pressed to handle any actions from your UIViewController. I prefer this technique over creating a protocol/delegate on the cell then calling back to the viewController.

@IBAction func pressedItem(sender: AnyObject) {
    let button = sender as! UIButton
    var indexPath = self.tableView.indexPathForView(button)!
    let object = self.arrayOfObjects[indexPath.row]
    // Now you can fire any action and have it related to that specific cell

// Create a HelpfulExtensions.swift class to hold your convenience extensions such as this

// MARK: - UITableView
extension UITableView {
    func indexPathForView (view : UIView) -> NSIndexPath? {
        let location = view.convertPoint(CGPointZero, toView:self)
        return indexPathForRowAtPoint(location)
    }
}
//

And might I add, EXTENSIONS ROCK!!!

Upvotes: 2

Kazuki Mann
Kazuki Mann

Reputation: 5

Instead of using two tableViews it may be enough to setup a header view with static content. Probably it's easy to do inside the same and only one table view controller. But also it can be done separately and then a table view below with dynamic content, all within a simple view controller

Upvotes: 0

Related Questions