点按后UitableView更改sectionView

时间:2014-08-22 04:44:40

标签: ios objective-c uitableview

如果我点击部分,tableview展开onlu当前此部分,并在sectionView中更改指示器颜色 如果我折叠部分 - 指示颜色有另一种颜色

问题 - 有时颜色的变化

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
    headerView.backgroundColor = [UIColor lightGrayColor];

    UIButton *buttonHeader = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
    [buttonHeader addTarget:self action:@selector(expandGroup:) forControlEvents:UIControlEventTouchUpInside];
    buttonHeader.tag = section;


    UIImageView *expandIndicator = [[UIImageView alloc] init];
    expandIndicator.frame = CGRectMake(270, 25, 30, 30);

    expandIndicator.backgroundColor = [UIColor darkGrayColor];

    if ([[_exerciseForGroup objectAtIndex:section] count] > 0) {
        expandIndicator.backgroundColor = [UIColor greenColor];
    }

    UILabel *labeHeader = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 60)];

    labeHeader.text = [[_groups objectAtIndex:section] objectForKey:@"nameExerciseGroup"];
    [labeHeader setFont:[UIFont systemFontOfSize:25]];

    [buttonHeader addSubview:labeHeader];
    [buttonHeader addSubview:expandIndicator];


    [headerView addSubview:buttonHeader];

    return headerView;
}
- (IBAction)expandGroup:(id)sender {

    UIButton *button = (UIButton *) sender;
    int section = [button tag];

    if ([[_exerciseForGroup objectAtIndex:section] count] == 0) {

        [self addAllExerciseFromGroupInSection:section];
    } else {

        [self deleteExerciseFromGroupInSection:section];

    }
}
- (void)addAllExerciseFromGroupInSection:(int)section {

    [_tableviewExercise reloadData];

    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

    for (int i = 0; i < [[_originalExercise objectAtIndex:section] count]; i++) {

        [[_exerciseForGroup objectAtIndex:section] addObject:[[_originalExercise objectAtIndex:section] objectAtIndex:i]];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:section];

        [indexPaths addObject:indexPath];
    }
    [_tableviewExercise beginUpdates];
    [_tableviewExercise insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [_tableviewExercise endUpdates];

    [_tableviewExercise scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];

    NSMutableArray *indexPathsForDeleteBefore = [[NSMutableArray alloc] init];
    for (int i = 0; i < section; i++) {

        int count = [[_exerciseForGroup objectAtIndex:i] count];

        for (int j = 0; j < count; j++) {

            [[_exerciseForGroup objectAtIndex:i] removeObjectAtIndex:0];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];

            [indexPathsForDeleteBefore addObject:indexPath];
        }

    }

    [_tableviewExercise beginUpdates];
    [_tableviewExercise deleteRowsAtIndexPaths:indexPathsForDeleteBefore withRowAnimation:UITableViewRowAnimationFade];
    [_tableviewExercise endUpdates];

    NSMutableArray *indexPathsForDeleteAfter = [[NSMutableArray alloc] init];
    for (int i = section + 1; i < [_groups count]; i++) {

        int count = [[_exerciseForGroup objectAtIndex:i] count];

        for (int j = 0; j < count; j++) {

            [[_exerciseForGroup objectAtIndex:i] removeObjectAtIndex:0];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];

            [indexPathsForDeleteAfter addObject:indexPath];
        }

    }

    [_tableviewExercise beginUpdates];
    [_tableviewExercise deleteRowsAtIndexPaths:indexPathsForDeleteAfter withRowAnimation:UITableViewRowAnimationFade];
    [_tableviewExercise endUpdates];
}


- (void)deleteExerciseFromGroupInSection:(int)section {

    [_tableviewExercise reloadData];
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];

    int count = [[_exerciseForGroup objectAtIndex:section] count];

    for (int i = 0; i < count; i++) {

        [[_exerciseForGroup objectAtIndex:section] removeObjectAtIndex:0];

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:section];
        [indexPaths addObject:indexPath];
    }

    [_tableviewExercise beginUpdates];
    [_tableviewExercise deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [_tableviewExercise endUpdates];
}

1 个答案:

答案 0 :(得分:1)

YourViewController.h

@interface AboutVC : UIViewController
{
    int section_expand;
}

YourViewController.m

- (void)viewDidLoad
{
    section_expand = -1;

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section_expand == section)
    {
        return [arr count];
    }
    else
    {
        return 0;

    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 80;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
    headerView.backgroundColor = [UIColor lightGrayColor];

    //UIButton *buttonHeader = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
    //[buttonHeader addTarget:self action:@selector(expandGroup:) forControlEvents:UIControlEventTouchUpInside];
    //buttonHeader.tag = section;

    UIButton *buttonHeader=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonHeader setFrame:CGRectMake(0, 0, 320, 80)];
    buttonHeader.tag=section;
    [buttonHeader addTarget:self action:@selector(expandGroup:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:buttonHeader];


    UIImageView *expandIndicator = [[UIImageView alloc] init];
    expandIndicator.frame = CGRectMake(270, 25, 30, 30);


    if (section_expand == section)
    {
        expandIndicator.backgroundColor = [UIColor greenColor];
    }
    else
    {
        expandIndicator.backgroundColor = [UIColor darkGrayColor];
    }


    UILabel *labeHeader = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 60)];

    labeHeader.text = [[_groups objectAtIndex:section] objectForKey:@"nameExerciseGroup"];
    [labeHeader setFont:[UIFont systemFontOfSize:25]];

    [buttonHeader addSubview:labeHeader];
    [buttonHeader addSubview:expandIndicator];


    [headerView addSubview:buttonHeader];

    return headerView;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.section];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = nil;
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        cell.backgroundColor = [UIColor clearColor];



        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{

}

-(IBAction)expandGroup:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    section_expand = btn.tag;

    [_TBL reloadData];
}