简体   繁体   中英

Adding sections and header UITableView

I have a UITableView using an accordion view. and id like to separate the parent cells into 2 sections with headers but not too sure how to go bout doing so as i have never used an accordion view before and the IB is eliminated as it is mainly code based. Code below:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return[topItems count] + ((currentExpandedIndex > -1) ? [[subItems objectAtIndex:currentExpandedIndex] count] : 0);
}

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


    // Define cell identifiers
    static NSString *ParentCellIdentifier       = @"ParentCell";
    static NSString *ChildCellIdentifier        = @"ChildCell";

    // Check to see if cell isChild
    BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

    // Initialise cell
    ACNBookingCell *cell;

    if (isChild && currentExpandedIndex == 0) {

    }
    else if (isChild) {
        cell = [tableView dequeueReusableCellWithIdentifier:ChildCellIdentifier];
    }
    else {
        cell = [tableView dequeueReusableCellWithIdentifier:ParentCellIdentifier];
    }
    if (cell == nil) {
        cell = [[ACNBookingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ParentCellIdentifier];
    }

    // Current Orders cell
    if (isChild && currentExpandedIndex == 0) {
        cell.textLabel.text = @"Child Cell";
    }
    // History cell
    else if (isChild && currentExpandedIndex == 2) {

    }
    // Favorites cell
    else if (isChild) {
        cell.textLabel.text = @"Child Cell";
    }
    // Parent cell
    else {
        int topIndex = (currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex) ? (int)indexPath.row - (int)[[subItems objectAtIndex:currentExpandedIndex] count] : (int)indexPath.row;
        cell.cellTitle.text = [topItems objectAtIndex:topIndex];

        NSLog(@"%ld",(long)indexPath.row);


        switch (indexPath.row) {
            case 0:
                cell.icon.image = [UIImage imageNamed:@"calendar"];
                break;
            case 1:
                cell.icon.image = [UIImage imageNamed:@"globe"];
                break;
            case 2:
                cell.icon.image = [UIImage imageNamed:@"taxi"];
                break;
            case 3:
                cell.icon.image = [UIImage imageNamed:@"hotel"];
                break;
            case 4:
                cell.icon.image = [UIImage imageNamed:@"calendar"];
                break;
            case 5:
                cell.icon.image = [UIImage imageNamed:@"taxi"];
                break;
        }
    }

    return cell;
}

#pragma mark - Table view delegate

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ACNBookingCell *cell = (ACNBookingCell *)[self.tableView cellForRowAtIndexPath:indexPath];

    // Check to see if cell isChild
    BOOL isChild = currentExpandedIndex > -1 && indexPath.row > currentExpandedIndex && indexPath.row <= currentExpandedIndex + [[subItems objectAtIndex:currentExpandedIndex] count];

    // Action behind Child cell
    if (isChild) {

        if (currentExpandedIndex == 0) {
            childIndexPath = indexPath;
        }
        else if (currentExpandedIndex == 2) {
            childIndexPath = indexPath;
        }
        return;
    }

    [self.tableView beginUpdates];

    // Expand parent cell to show child cells
    if (currentExpandedIndex == indexPath.row) {

        cell.arrowImage.image = [UIImage imageNamed:@"collapse"];

        [self collapseSubItemsAtIndex:currentExpandedIndex];
        currentExpandedIndex = -1;
    }
    // Collapse parent cell and hide child cells
    else {

        cell.arrowImage.image = [UIImage imageNamed:@"drop_down"];

        BOOL shouldCollapse = currentExpandedIndex > -1;

        if (shouldCollapse) {
            [self collapseSubItemsAtIndex:currentExpandedIndex];
        }

        currentExpandedIndex = (shouldCollapse && indexPath.row > currentExpandedIndex) ? (int)indexPath.row - (int)[[subItems objectAtIndex:currentExpandedIndex] count] : (int)indexPath.row;

        [self expandItemAtIndex:currentExpandedIndex];
    }

    [self.tableView endUpdates];
} 

You have to set the numberOfSections to 2 or anything u want. Then you can change the headers und die cell's in the section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM