创建可扩展表 iOS 的问题 - Objective-C [英] Issues Creating Expandable Table iOS - Objective-C

查看:28
本文介绍了创建可扩展表 iOS 的问题 - Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an expandable table view, however when I click on one of the cells I load a .xib file to format the subviews, however when I click the cell again the .xib format remains and messes with the view of the cells. Is there a better way to make the expandable table view?

代码:

- (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;
}

在此处展开​​和折叠:

Expand and Collapse here:

- (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文件来格式化门票

The main cell is a list of items and the expanding cell is suppose to be tickets from a sql database. I use a xib file to format the tickets

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

but when I collapse the cell which contains the items, this image remains and covers up the item cells

推荐答案

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

For creating expandable table view on clicking of UITableViewCell simply add a new cell in existing UITableView

[self.tableview insertRowsAtIndexPath:indexPathwithRowAnimation:UITableViewRowAnimationAutomatic];

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

这篇关于创建可扩展表 iOS 的问题 - Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆