UITableView Mix of Static and Dynamic Cells?

I know you cannot mix Static and Dynamic cell types in a single UITableView but I couldn't think of a better way to describe my issue.

I have several predetermined cells with fixed content, I also have an unknown number of cells with dynamic content which sits in the middle. So I want to my table to look something like this:

Fixed
Fixed
Fixed
Dynamic
Dynamic
Dynamic
Dynamic
Dynamic
Fixed
Fixed

So how exactly do you recommend I approach this in my cellForRowAtIndexPath method?

Thanks.

Answers


As you stated you can't mix static and dynamic cells. However, what you can do is break up the content into different data arrays that correspond to each group. Then break the table up into difference sections and load the data from the correct array in cellForRowAtIndexPath:.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"CELLID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

    switch (indexPath.section) {
        case 0:{
            cell.textLabel.text = self.arrayOfStaticThings1[indexPath.row];
        }break;

        case 1:{
            cell.textLabel.text = self.arrayOfDynamicThings[indexPath.row];
        }break;

        case 2:{
            cell.textLabel.text = self.arrayOfStaticThings2[indexPath.row];
        }break;

        default:
            break;
    }

    return cell;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:{
            return self.arrayOfStaticThings1.count;
        }break;

        case 1:{
            return self.arrayOfDynamicThings.count;
        }break;

        case 2:{
            return self.arrayOfStaticThings2.count;
        }break;

        default:
            return 0;
            break;
    }
}

Best and Easiest Way Yet

1) Place Two Container Views in the Header and in the Footer of a Dynamic TableView 2) Assign the Static TableViews to these Containers and Untick the "Scrolling Enabled"

Please check out my illustration at http://stackoverflow.com/a/22524085/1537178


suraj k thomas, Have a look on this structure of mine, and works perfectly. @firecast I didn't use constraints within this project, however I did set the height of the container based on the container's tableview height like so:

    ContainerViewController *containerVC = self.childViewController.firstObject;
    CGRect frame = self.containerView.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;
    frame.size.width = self.tableView.frame.size.width;
    frame.size.height = containerVC.tableView.contentSize.height;
    self.containerView.frame = frame;
    [self.tableView setTableHeaderView:self.containerView];

Hope this helps, let me know if you have any question.


Need Your Help

Google map marker disappears

javascript jquery html google-maps-api-3 google-maps-markers

I have an issue with my map marker. When you load the map the first time, everything works fine. However when the div is hidden and re-shown (and calling the same function map()), the map loads f...

Test accessibility with webdriver

selenium-webdriver automation webdriver uiaccessibility

I have web application and I need to make sure that all controls are reachable from keyboard.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.