如何使用Objective c创建多个tableview单元格?

时间:2015-12-15 07:06:54

标签: ios objective-c iphone uitableview

我使用LocalDate创建accordion tableview单元格。在这里,我在单个tableview中的单个故事板上维护了两个iOS storyboardtableview custom cell classes

现在我的问题是在选择基于cells存储的行之后它将在所选择的两个下扩展(添加)其他行。我需要为选定的tableview单元格plist和可扩展单元格(parent cell)使用单独的tableview单元格。通过我的下面的代码,父和子单元格显示并引用相同的单元格(那只是父类。所以我无法区分父母和子女的UI。

需要修改我的逻辑。

(child cell)

3 个答案:

答案 0 :(得分:1)

您需要设计两个不同的表格单元格视图,并在cellForRowAtIndexPath中实例化一个,具体取决于它是子项还是非子项。

非常

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

    id item = self.itemsInTable[indexPath.row];

    UITableViewCell *cell;

    if (item[@"isSubItem"]) {

        cell = [tableView dequeueReusableCellWithIdentifier:@"subItemCell"];

    } else {

        cell = [tableView dequeueReusableCellWithIdentifier:@"normalCell"];
    }

    // ...

    return cell;
}

答案 1 :(得分:1)

在以下代码中,我使用UITableView文件创建了多级可扩展.plist

.plist文件

中读取数据
NSDictionary *dTmp = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
self.arrayOriginal = [dTmp valueForKey:@"Objects"];

self.arForTable = [[NSMutableArray alloc] init];
[self.arForTable addObjectsFromArray:self.arrayOriginal];

UITableView代表和数据源的代码。

   // Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

// Customize the appearance of table view cells.
- (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:CellIdentifier] ;
    }

    cell.textLabel.text=[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"name"];
    [cell setIndentationLevel:[[[self.arForTable objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *d=[self.arForTable objectAtIndex:indexPath.row];
    if([d valueForKey:@"Objects"]) {
        NSArray *ar=[d valueForKey:@"Objects"];

        BOOL isAlreadyInserted=NO;

        for(NSDictionary *dInner in ar ){
            NSInteger index=[self.arForTable indexOfObjectIdenticalTo:dInner];
            isAlreadyInserted=(index>0 && index!=NSIntegerMax);
            if(isAlreadyInserted) break;
        }

        if(isAlreadyInserted) {
            [self miniMizeThisRows:ar];
        } else {
            NSUInteger count=indexPath.row+1;
            NSMutableArray *arCells=[NSMutableArray array];
            for(NSDictionary *dInner in ar ) {
                [arCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                [self.arForTable insertObject:dInner atIndex:count++];
            }
            [tableView insertRowsAtIndexPaths:arCells withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
}

-(void)miniMizeThisRows:(NSArray*)ar{

    for(NSDictionary *dInner in ar ) {
        NSUInteger indexToRemove=[self.arForTable indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"Objects"];
        if(arInner && [arInner count]>0){
            [self miniMizeThisRows:arInner];
        }

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

Download the sample code from here.

<强>输出

enter image description here

这有助于满足您的要求。

答案 2 :(得分:0)

DSNestedAccordion非常好地嵌套了tableViews。看看here