TableView可扩展/可折叠部分,某些部分不会扩展/崩溃



我已经实现了一些代码,以在我的表观视图部分添加可扩展/可折叠功能。可以从0到6个部分,取决于核心数据对象。我已经更新了viewForHeaderInSection方法,以包括根据截面名称的条款。在相同的方法中,我正在使用一个带有按钮的Uiview来扩展或折叠部分行。我还更新了高度forrowatIndExpath方法,以根据选择要展开/折叠的部分更改行高。

要测试应用程序,我只检查第0节和第1节。

我在此处放置了所有牵连方法的代码:

//viewForHeaderInSection method
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static NSString *header = @"customHeader";
   // UITableViewHeaderFooterView *vHeader;
    id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections] objectAtIndex:section];
    NSString *tmp = [theSection name];
    NSLog(@"SECTIONNAME = %@",tmp);
    UIView *mView = [[UIView alloc]initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 20.0f)];
    [mView setBackgroundColor:[UIColor redColor]];
    if ([tmp isEqualToString:@"0"]){
        NSLog(@"HAS ENTRADO EN SECTION 0");

    UIImageView *logoView = [[UIImageView alloc]initWithFrame:CGRectMake(285.0f, 2.0f,20.0f,20.0f)];
    [logoView setImage:[UIImage imageNamed:@"leftarrow.png"]];
    [mView addSubview:logoView];
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
    [bt setFrame:CGRectMake(20.0f, -4.0f, 150.0f, 30.0f)];
    [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bt setTag:0];
    [bt.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [bt.titleLabel setTextAlignment:NSTextAlignmentNatural];
    [bt.titleLabel setTextColor:[UIColor yellowColor]];
        NSString *valor = [NSString stringWithFormat:@"OVERDUE (%d)", [self.tableView
                                                                               numberOfRowsInSection:section]];
    [bt setTitle:valor forState: UIControlStateNormal];
    [bt addTarget:self action:@selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
    [mView addSubview:bt];
        return mView;
    }
    if ([tmp isEqualToString:@"1"]){
        NSLog(@"HAS ENTRADO EN SECTION 1");
        UIView *mView = [[UIView alloc]initWithFrame:CGRectMake(10.0f, 0.0f, 300.0f, 20.0f)];
        [mView setBackgroundColor:[UIColor orangeColor]];
        UIImageView *logoView = [[UIImageView alloc]initWithFrame:CGRectMake(285.0f, 2.0f,20.0f,20.0f)];
        [logoView setImage:[UIImage imageNamed:@"leftarrow.png"]];
        [mView addSubview:logoView];
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        [bt setFrame:CGRectMake(20.0f, -4.0f, 150.0f, 30.0f)];
        [bt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        NSLog(@"SECTION 1 EN SEGUNDA LECTURA");
        [bt setTag:1];
        [bt.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [bt.titleLabel setTextAlignment:NSTextAlignmentNatural];
        [bt.titleLabel setTextColor:[UIColor yellowColor]];
        NSString *valor = [NSString stringWithFormat:@"TODAY   (%d)", [self.tableView
                                                                               numberOfRowsInSection:section]];
        [bt setTitle:valor forState: UIControlStateNormal];
        [bt addTarget:self action:@selector(addCell1:) forControlEvents:UIControlEventTouchUpInside];
        [mView addSubview:bt];
        return mView;
    }
    return mView;
}


// heightForRowAtIndexPath method

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        return heightOfSection0;NSLog(@"HAS PULSADO SECTION 0 CERRADA");
    }
    else {
        return 50.0f;NSLog(@"HAS PULSADO SECTION 0 ABIERTA");
    }
    if (indexPath.section == 1){
        return heightOfSection1;NSLog(@"HAS PULSADO SECTION 1 CERRADA");
    }
    else {
        return 50.0f;NSLog(@"HAS PULSADO SECTION 0 ABIERTA");
    }
   }

现在按钮操作方法:

- (void)addCell:(UIButton *)bt{
    // If section of more information
    if(bt.tag == 0) {
        NSLog(@"HAS PULSADO SECTION 0 EN ADDCELL");
        // Initially more info is close, if more info is open
        if(isSection0Open) {

            // Set height of section
            heightOfSection0 = 45.0f;
            // Reset the parameter that more info is closed now
            isSection0Open = NO;
        }else if (isSection0Open==NO){
            // Set height of section
            heightOfSection0 = 0.0f;
            // Reset the parameter that more info is closed now
            isSection0Open = YES;
        }
    }

    [self.tableView reloadData];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

}// end addCell
- (void)addCell1:(UIButton *)bt{
    // If section of more information
    if(bt.tag == 1) {
        NSLog(@"HAS PULSADO SECTION 1 EN ADDCELL1");
        // Initially more info is close, if more info is open
        if(isSection1Open) {

            // Set height of section
            heightOfSection1 = 45.0f;
            // Reset the parameter that more info is closed now
            isSection1Open = NO;
        }else if (isSection1Open==NO){
            // Set height of section
            heightOfSection1 = 0.0f;
            // Reset the parameter that more info is closed now
            isSection1Open = YES;
        }
    }

    [self.tableView reloadData];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];

}// end addCell1

第0节没有问题,它会按预期扩展和崩溃。问题在于第1节,我想当我尝试在其余部分实现相同的代码时,也会发生同样的问题...

欢迎任何帮助。谢谢。

我检测到了我的错误,它在高度forrowatIndExpath方法上。这是在我的应用中写入它的正确方法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0){
        return heightOfSection0;
    }
   else if (indexPath.section == 1){

       return heightOfSection1;
    }
    else {
        return 45.0f;
    }
}

那是。在任何情况下都谢谢您。

最新更新