gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:22:29 24 4
gpt4 key购买 nike

我正在使用 iOS Storyboard 创建 accordion tableview 单元格。在这里,我在单个 tableview 的单个 Storyboard上维护了两个 tableview 自定义单元格类cells

现在我的问题是在选择了基于 plist 存储的行之后,它会在选定的两个下扩展(添加)额外的行。我需要为选定的表格 View 单元格 (父单元格) 和可扩展单元格 (子单元格) 使用单独的表格 View 单元格。通过我下面的代码,父单元和子单元显示并引用相同的单元(那只是父单元。所以我无法区分父单元和子单元的 UI。

需要修改下面我的逻辑。

-(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
{
// NEED TO ADD SECOND CELL HERE
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];
}
}
}

最佳答案

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

.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 委托(delegate)和数据源的代码。

   // 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

希望这有助于满足您的要求。

关于ios - 如何使用 Objective c 创建多个 tableview 单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34282985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com