创建可扩展表iOS的问题 - Objective-C

时间:2017-06-28 14:08:03

标签: ios objective-c uitableview

我正在尝试创建一个可扩展的表视图,但是当我单击其中一个单元格时,我加载.xib文件以格式化子视图,但是当我再次单击该单元格时,.xib格式仍然存在并且与视图混淆细胞有没有更好的方法来制作可扩展的表格视图?

代码:

- (BOOL)tableView:(UITableView *)tableView canCollapseSection:(NSInteger)section
{
    return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 28;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self tableView:tableView canCollapseSection:section])
    {
        if ([expandedSections containsIndex:section])
        {
            return [ticketList count];
        }
        return 1; // only top row showing
    }

    // Return the number of rows in the section.
    return 1;
}

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

在此处加载xib:

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

    static NSString *CellIdentifier = @"Cell";

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

    // Configure the cell...

    NSLog(@"Number: %d", indexPath.section);
    NSArray *items = [[NSArray alloc] initWithObjects:@"Cleanliness", @"Door", @"Peek Hole", @"Sink", @"Towel Rack", @"Closet", @"Carpet", @"Wall", @"Bed", @"Matress", @"Mattress Cover", @"Fridge", @"Blinds", @"Window", @"Screen", @"Air Conditioning", @"Chair", @"Desk", @"Garbage bin", @"Shelves", @"Phone", @"Jacks", @"Lights", @"Smoke Detector", @"Heat Detector", @"Light bulb", @"Deep Cleaning", @"Final Prep", nil];

    if (!indexPath.row)
    {
        // first row
        cell.textLabel.text = items[indexPath.section]; // only top row showing

        if ([expandedSections containsIndex:indexPath.section])
        {

        }
        else
        {

        }
    }
    else
    {
        [self.tableView registerNib:[UINib nibWithNibName:@"HelpDeskCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
        static NSString *CellIdentifier = @"Cell";
        HelpDeskCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(cell == nil){
            cell = [[HelpDeskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

        CGFloat fontsize = 16;

        if([[[ticketList objectAtIndex:indexPath.row] objectForKey:@"priority"] isEqualToString:@"Critical"]){

            [cell.IDLabel setBackgroundColor:[UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1.0]];
        }
        else{
            [cell.IDLabel setBackgroundColor:[UIColor colorWithRed:0.925 green:0.925 blue:0.925 alpha:1.0]];
        }
        [cell.IDLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];



        //reduce fontsize to 12 for the information labels
        //same on all devices and orientations
        fontsize = 12;

        //ticket status label
        [cell.statusLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
        [cell.statusLabel setTextColor:[UIColor whiteColor]];


        //ticket category label
        [cell.categoryLabel setBackgroundColor:[UIColor grayColor]];
        [cell.categoryLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
        [cell.categoryLabel setTextColor:[UIColor whiteColor]];


        //ticket title label
        [cell.titleLabel setFont:[UIFont boldSystemFontOfSize:fontsize]];
        [cell.titleLabel setTextColor:[UIColor blackColor]];

        //Label holds the user that submitted the ticket
        [cell.submittedLabel setFont:[UIFont systemFontOfSize:fontsize]];
        [cell.submittedLabel setTextColor:[UIColor blackColor]];

        //Label holds the user currently working on the ticket
        [cell.handleLabel setFont:[UIFont systemFontOfSize:fontsize]];
        [cell.handleLabel setTextColor:[UIColor blackColor]];

        //ticket date label
        [cell.dateLabel setFont:[UIFont systemFontOfSize:fontsize]];



        // Set the text of the subviews
        NSString * ticketIdStr = [[ticketList objectAtIndex:indexPath.row] objectForKey:@"ticket_id"];
        [cell.IDLabel setText:ticketIdStr];
        NSString * ticketStatusStr = [[ticketList objectAtIndex:indexPath.row] objectForKey:@"status"];
        [cell.statusLabel setText:ticketStatusStr];
        if([ticketStatusStr isEqualToString:@"Open"]) {
            [cell.statusLabel setBackgroundColor: [UIColor colorWithRed:0 green:0.6 blue:0.8 alpha:1.0]];
        }
        else if ([ticketStatusStr isEqualToString:@"In Progress"]) {
            [cell.statusLabel setBackgroundColor: [UIColor colorWithRed:1.0 green:0.733 blue:0.2 alpha:1.0]];
        }
        else if ([ticketStatusStr isEqualToString:@"Resolved"]) {
            [cell.statusLabel setBackgroundColor: [UIColor colorWithRed:0.6 green:0.8 blue:0 alpha:1.0]];
        }
        else if ([ticketStatusStr isEqualToString:@"Closed"]) {
            [cell.statusLabel setBackgroundColor: [UIColor colorWithRed:0.8 green:0 blue:0 alpha:1.0]];
        }
        NSString * categoryStr = [[ticketList objectAtIndex:indexPath.row] objectForKey:@"category"];
        [cell.categoryLabel setText:categoryStr];
        NSString * titleStr = [[ticketList objectAtIndex:indexPath.row] objectForKey:@"title"];
        NSString * userIDStr = [[ticketList objectAtIndex:indexPath.row] objectForKey:@"user_id"];
        NSString * handledByStr = [[ticketList objectAtIndex:indexPath.row] objectForKey:@"handled_by"];
        [cell.titleLabel setText: [NSString stringWithFormat:@"Title: %@", titleStr]];
        [cell.submittedLabel setText: [NSString stringWithFormat:@"Submitted By: %@", userIDStr]];
        [cell.handleLabel setText: [NSString stringWithFormat:@"Handled By: %@", handledByStr]];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        NSDate *orignalDate = [dateFormatter dateFromString:[[[ticketList objectAtIndex:indexPath.row] objectForKey:@"date_submitted"] substringToIndex:10]];

        [dateFormatter setDateFormat:@"MMMM dd, yyyy"];
        NSString * ticketDateStr = [dateFormatter stringFromDate:orignalDate];

        [cell.dateLabel setText:ticketDateStr];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    return cell;
}

在此展开和折叠:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self tableView:tableView canCollapseSection:indexPath.section])
    {
        if (!indexPath.row)
        {
            // only first row toggles exapand/collapse
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

            NSInteger section = indexPath.section;
            BOOL currentlyExpanded = [expandedSections containsIndex:section];
            NSInteger rows;

            NSMutableArray *tmpArray = [NSMutableArray array];

            if (currentlyExpanded)
            {
                rows = [self tableView:tableView numberOfRowsInSection:section];
                [expandedSections removeIndex:section];

            }
            else
            {
                [expandedSections addIndex:section];
                rows = [self tableView:tableView numberOfRowsInSection:section];
            }

            for (int i=1; i<rows; i++)
            {
                NSIndexPath *tmpIndexPath = [NSIndexPath indexPathForRow:i
                                                               inSection:section];
                [tmpArray addObject:tmpIndexPath];
            }

            UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

            if (currentlyExpanded)
            {
                [tableView deleteRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];

            }
            else
            {
                [tableView insertRowsAtIndexPaths:tmpArray
                                 withRowAnimation:UITableViewRowAnimationTop];

            }
        }
        [self.tableView reloadData];

    }
}

更新

主单元格是一个项目列表,扩展单元格被认为是来自sql数据库的票证。我使用xib文件格式化票证

xib file

但是当我折叠包含项目的单元格时,此图像将保留并覆盖项目单元格

1 个答案:

答案 0 :(得分:0)

要在单击UITableViewCell时创建可扩展表视图,只需在现有UITableView中添加一个新单元格

[self.tableview insertRowsAtIndexPath:indexPath  withRowAnimation:UITableViewRowAnimationAutomatic];