Adding unknown number of rows to 'Static Cells' UITableView

后端 未结 7 1635
孤城傲影
孤城傲影 2020-11-28 04:07

I have a static table created in Interface Builder with 6 sections all with different amounts of rows. I now want to add a 7th section with a varying number of rows.

7条回答
  •  离开以前
    2020-11-28 04:44

    I thought I'd add an updated answer based on @Darren's excellent answer. Most of the delegate methods are not required. So, I just added the required ones. You can easily add a custom cell if you wish, even using a nib file. The image shows a static table with 3 sections. The final section is run time dynamic. This is extremely handy. This is working in ios7 BTW.

    enter image description here

    #define DYNAMIC_SECTION 2
    
    #import "MyTableViewController.h"
    
    @interface MyTableViewController ()
    @property (strong, nonatomic)NSArray *myArray;
    @end
    
    @implementation MyTableViewController
    
    - (id)initWithCoder:(NSCoder *)aDecoder
        {
            if (self = [super initWithCoder:aDecoder]) {
                _myArray = @[@"ONE", @"TWO", @"THREE", @"FOUR"];
            }
            return self;
        }
    
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
        {
            return [super numberOfSectionsInTableView:tableView];
        }
    
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            if (section != DYNAMIC_SECTION) {
                return [super tableView:tableView numberOfRowsInSection:section];
            }
            return [self.myArray count];
        }
    
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            if (indexPath.section != DYNAMIC_SECTION) {
                return [super tableView:tableView cellForRowAtIndexPath:indexPath];
            }
            static NSString *id = @"MyCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:id];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:id];
            }
            cell.textLabel.text = self.myArray[indexPath.row];
            return cell;
        }
    
            // required
        -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            int section = indexPath.section;
            if (section == DYNAMIC_SECTION) {
                return [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
            } else {
                return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
            }
        }
    
                // Not required
        - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
        {
            if (section != DYNAMIC_SECTION) {
                return [super tableView:tableView titleForHeaderInSection:section];
            }
            return @"some title";
        }
    

提交回复
热议问题