簡體   English   中英

tableView可擴展/可折疊部分,某些部分不會擴展/折疊

[英]tableView expandable/collapsable sections, some sections do not expand/collapse

我已經實現了一些代碼,以將可擴展/可折疊功能添加到tableView部分。 取決於核心數據對象,可以有0到6個部分。 我已經更新了viewForHeaderInSection方法,以包含取決於節名稱的if子句。 在同一方法中,我使用帶有按鈕的UIView來展開或折疊部分行。 我還更新了heightForRowAtIndexPath方法,以根據選擇要展開/折疊的部分來更改行高。

要測試該應用程序,我只檢查第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部分,我想當我嘗試為其余部分實現相同的代碼時也會發生相同的問題...

歡迎任何幫助。 謝謝。

我檢測到我的錯誤,它是在heightForRowAtIndexPath方法上。 這是在我的應用中編寫它的正確方法:

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


    if (indexPath.section == 0){

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


       return heightOfSection1;
    }
    else {
        return 45.0f;
    }

}

是的 無論如何謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM