简体   繁体   中英

If we want static cell as well dynamic cells in same table view controller ios, how it can be possible in ios?

I have a tableViewController, and under that i want one static cell and rest of all will be dynamic cells . I have already run for dynamic cells , but within same tableViewController i also need to add one static cell, how can i achieve it?

Please Help

Thanks in advance.

you could do something like the following:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dynamicContent.count + 1; // +1 for the static cell at the beginning
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        // static cell
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StaticCellIdentifier" forIndexPath:indexPath];
        // customization
        return cell;
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DynamicCellIdentifier" forIndexPath:indexPath];
    id contentObject = self.dynamicContent[indexPath.row];
    // customization
    return cell;
}

You cannot create static and dynamic cell at same time in a UITableViewController. But you can hard code your static cell's data and load the data each time you reload your tableview.

You can keep all your cells in one section and keep checking for index path.row == 0 or create separate sections for them.

typedef NS_ENUM(NSUInteger, TableViewSectionType) {
    TableViewSectionType_Static,
    TableViewSectionType_Dynamic
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2; // One for static cell, and another for dynamic cells
}

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    switch(section) {
        case TableViewSectionType_Static:
            return 1; // Always return '1' to show the static cell at all times.
        case TableViewSectionType_Dynamic:
            return [myDynamicData count];
    }
}

With this approach your cells will be split into two sections and it will be easier to manage. And it will always show one cell, as number of rows returned for TableViewSectionType_Static is 1 always. It will show the dynamic cells based on your data count.

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