从表视图插入单元格和节

时间:2016-04-05 15:28:52

标签: objective-c cell tableviewcell sections

救救我!我无法在部分tableviews中插入单元格!

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
            NSInteger result = 0;
            result = [[_dictionoryOfDay allKeys] count];
            return result;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
       NSInteger result = 0;
        NSString *sectionNameInDictionary = [[_dictionoryOfDay allKeys]
                                         objectAtIndex:section];
        NSArray *sectionArray = [_dictionoryOfDay objectForKey:
                             sectionNameInDictionary];
        result = [sectionArray count];
        return result;
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        UICustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellMain" forIndexPath:indexPath];
        [cell.btnAddMore addTarget:self action:@selector(actionAddMore:)forControlEvents:UIControlEventTouchUpInside];
        cell.btnAddMore.tag = indexPath.section;
        return cell;
    }

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        TableHeaderAvailabel *header;
        if(!header){
              header = [tableView  dequeueReusableHeaderFooterViewWithIdentifier:@"TableHeaderAvailabel"];
         }
         NSString *result = nil;
         result = [[self.dictionoryOfDay allKeys] objectAtIndex:section];
         header.lbTitle.text = result;
         return header;
    }

    -(void)actionAddMore:(UIButton *)sender{

    }

1 个答案:

答案 0 :(得分:1)

我的猜测是,当您点按某个单元格btnAddMore时,您想要在该部分添加。如果是这种情况,那么actionAddMore()的实现应该看起来像

    NSString *sectionNameInDictionary = [[_dictionoryOfDay allKeys]
                                     objectAtIndex:section];
    NSArray *sectionArray = [_dictionoryOfDay objectForKey:
                         sectionNameInDictionary];
    // Create whatever you use to populate rows
    [sectionArray addObject: <your new object>];
    _dictionaryOfDay[sectionNameInDictionary] = sectionArray;
    [self.tableView reloadData];    // Or just load the new cell, but this is easier