gpt4 book ai didi

ios - 在 UITableView 部分添加小节

转载 作者:行者123 更新时间:2023-11-29 12:07:33 26 4
gpt4 key购买 nike

搜索了很多,但都是徒劳的。我有一个 NSMutableDictionary 的嵌套 NSMutableArray。我想在 UITableView 的部分中包含部分。以下是我的数组:

    <__NSArrayM 0x7ffe4267efb0>(
{
"group_title" = "Seller Information";
"group_values" = (
{
key = "First Name";
value = test;
},
{
key = "Last Name";
value = testl;
}
);
},
{
"group_title" = "Buyer Information";
"group_values" = (
{
key = "First Name";
value = Demo1;
},
{
key = "Last Name";
value = Demo;
}
);
},
{
"group_title" = "Transaction Information";
"group_values" = (
{
key = Status;
value = Active;
},
{
key = "MLS #";
value = "15-284";
},
{
key = Address;
value = "1101 Fargo Ave";
},
{
key = County;
value = Dickinson;
},
{
key = Zipcode;
value = 51360;
},
{
key = Contingencies;
value = (
{
key = "General Inspection";
value = (
{
key = "Contingency Verbiage";
value = "Inspection Results and balance of this paragraph";
}
);
},
{
key = "New Construction";
value = (
{
key = "Contingency Group";
value = "If Required";
},
{
key = "Days Until Due";
value = 5;
}
);
}
);
}
);
}
)

如果遍历上面的数组,那么group_title就是我想要的sections的个数。 key,group_values中的'value'是每个部分的行数。

对于键 Contingencies,存在嵌套数据。所以我想显示如下:

Transaction Information // section title
Contingencies // sub-section title
General Inspection // sub-sub-section title
Contingency Verbiage //value
New Construction // sub-sub-section title
Contingency Group //value
Days Until Due //value



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [dictData count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *arr = [[dictData valueForKey:@"group_values"] objectAtIndex:section];
NSUInteger numberOfRows = arr.count; // For second level section headers
for (id row in arr) {
if([[row objectForKey:@"value"] isKindOfClass:[NSMutableArray class]]) {
numberOfRows += [[row valueForKey:@"value"] count];
}
}
return numberOfRows;
}

我知道我必须仅将小节作为单元格进行操作,但不确定如何操作。

如何访问cellforrowatindexpath中的子部分?

请帮忙。我如何解决和实现这个问题?

最佳答案

您可以使用委托(delegate)方法 indentationLevelForRowAtIndexPath 向单元格添加缩进级别,因此如果您想要显示子部分,则只需返回一个 NSInteger 值,例如 1 ,返回值表示指定行的深度,以显示其在节中的层次位置。

到目前为止的例子返回值应该是

  Contingencies // sub-section title - 1
General Inspection // sub-sub-section title - 2
Contingency Verbiage //value - 3
New Construction // sub-sub-section title - 2
Contingency Group //value - 3
Days Until Due //value - 3

添加示例代码。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
//Set the indentation width, so that each indentation level width will increase by it.
cell.indentationWidth = 5;

//Show the value from corresponding indexPath
NSArray *arr = [[dictData valueForKey:@"group_values"] objectAtIndex:indexPath.section];
NSMutableArray *items = [NSMutableArray arrayWithArray:arr];
// For second level section headers
for (id row in arr) {
if([[row objectForKey:@"value"] isKindOfClass:[NSMutableArray class]]) {
[items addObjectsFromArray:[row valueForKey:@"value"]];
}
}
NSDictionary *item = items[indexPath.row]
cell.textLabel.text = item[@"key"];

return cell;

}
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {

//Retrive the right indentation for item at indexPath
NSArray *arr = [[dictData valueForKey:@"group_values"] objectAtIndex:indexPath.section];
if (indexPath.row < arr.count) {
return 0
}
// For second level section headers
return 1
}

关于ios - 在 UITableView 部分添加小节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34507886/

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