gpt4 book ai didi

ios - 如何使用 objective-c 使用两个 NSMutableArray 填充可扩展的 TableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:41:01 24 4
gpt4 key购买 nike

我正在使用 Expandable UITableview由汤姆·福斯特创建。我想使用两个 NSMutableArray 来调整这个例子,如果有人想从 webservice json 数据填充一个可扩展/折叠的 TreeView 表,那么这个场景就是想要实现的。因此,由于在他的示例中 GroupCell 没有数组,我想知道我该怎么做?请记住,我的 Objective-C 仍然生锈,因此,我在问这个问题。

我的尝试只是显示该组的第一个 ObjectAtIndex:indexPath:0。

我希望能够填充表格并获得这样的输出;

  • A组
    • 第 1a 行
    • 第 2a 行
    • 第 3a 行
  • B组
    • 第 1b 行
    • 第 2b 行
  • C组
    • 第 1c 行
    • 第 2c 行
    • 第 3c 行等等。

如果您以这种方式更好地理解,您也可以使用 JSON 数据来解释您的答案。

在这里,我想用 JSON 数据填充表格,以便 GroupCell 显示 class_name,rowCell 显示 subject_name。这是我从 JSON 网络服务解析内容的控制台;

(
{
"class_id" = 70;
"class_name" = Kano;
subject = (

"subject_id" = 159;
"subject_name" = "Kano Class";
}
);
},
{
"alarm_cnt" = 0;

"class_id" = 71;
"class_name" = Lagos;
subject = (

"subject_id" = 160;
"subject_name" = "Lagos Class";
}
);
},
{
"alarm_cnt" = 3;
"class_id" = 73;
"class_name" = Nasarawa;
subject = (

"subject_id" = 208;
"subject_name" = "DOMA Class";
},

"subject_id" = 207;
"subject_name" = "EGGON Class";
},

"subject_id" = 206;
"subject_name" = "KARU Class";
},

"subject_id" = 209;
"subject_name" = "LAFIA Class";
},

"subject_id" = 161;
"subject_name" = "Nasarawa State Class";
}
);
},
{
"alarm_cnt" = 2;
"class_id" = 72;
"class_name" = Rivers;
subject = (

"subject_id" = 162;
"subject_name" = "Rivers Class";
}
);
}

)

我试过这是我的代码片段

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{ static NSString *CellIdentifier = @"RowCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

NSDictionary *d=[_sitesJson objectAtIndex:0] ;
NSArray *arr=[d valueForKey:@"subject_name"];
NSDictionary *subitems = [arr objectAtIndex:0];
NSLog(@"Subitems: %@", subitems);
NSString *siteName = [NSString stringWithFormat:@"%@",subitems];
cell.textLabel.text =siteName;
//}
NSLog(@"Row Cell: %@", cell.textLabel.text);
// just change the cells background color to indicate group separation
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
cell.backgroundView.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:243.0/255.0 blue:1.0 alpha:1.0];

return cell;

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section

{ static NSString *CellIdentifier = @"GroupCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *textLabel = (UILabel *)[cell viewWithTag:2];
NSDictionary *d2 = [_regionsJson objectAtIndex:0];
NSArray *arr2 = [d2 objectForKey:@"class_name"];
NSString *regions = [[arr2 objectAtIndex:section]objectAtIndex:0];
textLabel.textColor = [UIColor whiteColor];
textLabel.text = [NSString stringWithFormat: @"%@ (%d)", regions, (int)[self tableView:tableView numberOfRowsInSection:section]];
NSLog(@"Group cell label: %@", textLabel.text);

// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:@"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) {
accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
accessoryView.transform = CGAffineTransformMakeRotation(0);
}
return cell;

最佳答案

他,只需要一点点更新一个方法

- (UITableViewCell *)tableView:(ExpandableTableView *)tableView cellForGroupInSection:(NSUInteger)section
{
static NSString *CellIdentifier = @"GroupCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSIndexPath *indexPath;
NSString *regions = [[_dataGroup objectAtIndex:section]objectAtIndex:0];
cell.textLabel.text = [NSString stringWithFormat: @"%@ ", regions];

// We add a custom accessory view to indicate expanded and colapsed sections
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ExpandableAccessoryView"] highlightedImage:[UIImage imageNamed:@"ExpandableAccessoryView"]];
UIView *accessoryView = cell.accessoryView;
if ([[tableView indexesForExpandedSections] containsIndex:section]) {
accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
accessoryView.transform = CGAffineTransformMakeRotation(0);
}
return cell;
}

可能会帮助你。

HTH,享受编码吧!

关于ios - 如何使用 objective-c 使用两个 NSMutableArray 填充可扩展的 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667542/

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