简体   繁体   中英

Mix of static and dynamic table view cells iOS

I was trying to create a table view with static and dynamic prototype cells.

So, the thing I want to achieve is like this:

static
static
static
static
dynamic
...

This post on Stackoverflow , UITableView Mix of Static and Dynamic Cells? , was kind of helpful but it couldn't cover my whole problem.

I want 4 static cells, but also I would like to include controls inside those cells and connect them as outlets, and then add more dynamic cells below.

So is this possible?

Thank you in advance.

  1. Create a table view with only static cells and then add a new table view inside the original one. However, this approach doesn't look professional, and moreover, when I added datasource and delegate to that new table view, the original table view moved down.

  2. Create two table views for static and dynamic cells, and then include them in one view. This is I think the plausible way, but because I'm a novice developer, couldn't find out how to solve the "static table views should be embedded within a table view controller".

You can subclass UITableViewController and override the dataSource and delegate methods to achieve this nicely.

The key is to forward calls on to super where the static content is required and do your own thing when dynamic content is required.

For example, use IB to define a UITableViewController with your static content in the normal way but then subclass as follows to add a single extra section with dynamic content:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [super numberOfSectionsInTableView:tableView] + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section < [super numberOfSectionsInTableView:tableView]) {
        return [super tableView: tableView numberOfRowsInSection:section];
    }
    else {
        return self.numberOfRowsInMyDynamicSection;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section < [super numberOfSectionsInTableView:tableView]) {
        return [super tableVIew: tableView cellForRowAtIndexPath: indexPath];
    }
    else {
        // do your own dynamic cell management;
    }
}

//etc. for the other dataSource and delegate methods

I've used this technique to create a UITableViewController subclass that allows you to show/hide statically defined cells at runtime.

It takes a bit of effort to implement all the dateSource/delegate methods, but you end up with a really handy UITableViewController subclass.

I would create the static cells as prototypes, but give them each a different cellReuseIdentifier. Then in your cellForRowAtIndexPath, dequeue using the right cellReuseIdentifier for the row. For the rows with dynamic data, offset the index by 4 in order to access the correct data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM