如何使用iOS Storyboard在单个tableview中创建两个自定义tableview单元格?

时间:2015-12-08 14:01:48

标签: ios objective-c uitableview

我正在尝试使用iOS故事板在单个tableview内创建具有单独tableviewCell的accordion tableview。下面我添加了我的测试代码。请帮我解决问题。

我的来源:

Tableview方法我从属性列表中获取的索引数据

#pragma mark - Table view data source

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.itemsInTable count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *Title= [[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Name"];
        return [self createCellWithTitle:Title image:[[self.itemsInTable objectAtIndex:indexPath.row] valueForKey:@"Image name"] indexPath:indexPath];
    }

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *dic=[self.itemsInTable objectAtIndex:indexPath.row];
        if([dic valueForKey:@"SubItems"])
        {
            NSArray *arr=[dic valueForKey:@"SubItems"];
            BOOL isTableExpanded=NO;

            for(NSDictionary *subitems in arr )
            {
                NSInteger index=[self.itemsInTable indexOfObjectIdenticalTo:subitems];
                isTableExpanded=(index>0 && index!=NSIntegerMax);
                if(isTableExpanded) break;
            }

            if(isTableExpanded)
            {
                [self CollapseRows:arr];
            }
            else
            {
                NSUInteger count=indexPath.row+1;
                NSMutableArray *arrCells=[NSMutableArray array];
                for(NSDictionary *dInner in arr )
                {
                    [arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                    [self.itemsInTable insertObject:dInner atIndex:count++];
                }
                [self.main_tableview insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationNone];
            }
        }
    }

CollapseRow流程

-(void)CollapseRows:(NSArray*)ar
{
    for(NSDictionary *dInner in ar )
    {
        NSUInteger indexToRemove=[self.itemsInTable indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"SubItems"];
        if(arInner && [arInner count]>0)
        {
            [self CollapseRows:arInner];
        }

        if([self.itemsInTable indexOfObjectIdenticalTo:dInner]!=NSNotFound)
        {
            [self.itemsInTable removeObjectIdenticalTo:dInner];
            [self.main_tableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:
                                [NSIndexPath indexPathForRow:indexToRemove inSection:0]
                                                        ]
                                      withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}

Tableview用户界面。同样的UI也出现在子tableview单元格中。我需要为父和子tableview单元格显示单独的UI

- (UITableViewCell*)createCellWithTitle:(NSString *)title image:(UIImage *)image  indexPath:(NSIndexPath*)indexPath
{
    NSString *CellIdentifier_one = @"Cell_One";

    cell_one = [self.main_tableview dequeueReusableCellWithIdentifier:CellIdentifier_one];

    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithRed:54/255.0f green:169/255.0f blue:224/255.0f alpha:0.40f];
    cell_one.backgroundColor = [UIColor colorWithRed:54/255.0f green:169/255.0f blue:224/255.0f alpha:0.25f];
    cell_one.selectedBackgroundView = bgView;
    cell_one.titlelbl.text = title;
    cell_one.titlelbl.textColor = [UIColor blackColor];

    return cell_one;
}

1 个答案:

答案 0 :(得分:0)

你有indexPath。您必须知道哪个单元格用于哪个indexPath。

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    id cell;
    if(indexPath.row==0) {
        cell = [self createCell];
    }
    else {
        cell = [self createOtherCell];
    }
    return cell;
}