Adding unknown number of rows to 'Static Cells' UITableView

后端 未结 7 1634
孤城傲影
孤城傲影 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:47

    To add dynamic cells to a static cells table you have to override every UITableView delegate method that has an indexPath.

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    
    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    
    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
    
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    .

    -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
         return NO;
    }
    
    -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
    {     
         return NO;
    }
    
    -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {     
         return UITableViewCellEditingStyleNone;     
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         int section = indexPath.section;
    
         // if dynamic section make all rows the same height as row 0
         if (section == self.dynamicSection) {
              return [super tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
         } else {
              return [super tableView:tableView heightForRowAtIndexPath:indexPath];
         }
    }
    
    - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         int section = indexPath.section;
    
         // if dynamic section make all rows the same indentation level as row 0
         if (section == self.dynamicSection) {
              return [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];
         } else {
              return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
         }
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
         if (section == self.dynamicSection ) {
              return [self.dataListArray count];
         } else {
              return [super tableView:tableView numberOfRowsInSection:section];
         }
    }
    
    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
    {
         int section = indexPath.section;
         int row = indexPath.row;
    
    
         if (section == self.dynamicSection) {
              // make dynamic row's cell
              static NSString *CellIdentifier = @"Dynamic Cell";
              UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
              if (!cell) {
                   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
              }
    
              cell.textLabel.text = [self.dataListArray objectAtIndex:row];
              return cell;
        } else {
              return [super tableView:tableView cellForRowAtIndexPath:indexPath];
        }
    }
    

    Only once you have every method overridden will your table start to work. For any referencing the static section, just refer to [super].

    0 讨论(0)
提交回复
热议问题