gpt4 book ai didi

ios - 将 NSDictionary 转换为 UITableView

转载 作者:行者123 更新时间:2023-11-29 00:40:23 25 4
gpt4 key购买 nike

我正在尝试将 NSDictionary 数据放入 TableView 中。但我在访问值时遇到问题。

字典看起来是这样的:

multimedia =     {
uploads = {
upload = (
{
FileName = {
text = "\n 1-FB5A9792.MOV";
};
status = {
text = "\n 2";
};
Event = {
text = "\n Test";
};
Date = {
text = "\n 9/7/2016 9:06:21 AM";
};
Publicacion = {
text = "\n Example";
};
Seccion = {
text = "\n Sistemas";
};
id = {
text = "\n \n \n 993";
};
text = "\n ";
},
{
FileName = {
text = "\n 2-FB5A9793.MOV";
};
status = {
text = "\n 2";
};
Event = {
text = "\n Test";
};
Date = {
text = "\n 9/7/2016 9:06:21 AM";
};
Publicacion = {
text = "\n Example";
};
Seccion = {
text = "\n Sistemas";
};
id = {
text = "\n \n 994";
};
text = "\n ";
},
{
FileName = {
text = "\n 1-IMG_0006.MOV";
};
status = {
text = "\n 2";
};
Event = {
text = "\n Highest";
};
Date = {
text = "\n 9/7/2016 4:56:37 PM";
};
Publicacion = {
text = "\n Example";
};
Seccion = {
text = "\n Sistemas";
};
id = {
text = "\n \n 995";
};
text = "\n ";
},...

我使用 XMLReader 将 XML 转换为字典,然后将上述数据传递给 tableData

 tableData = [jsonDictionary allKeys];

表格

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

创建单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
NSString *key = [tableData objectAtIndex:indexPath.row];
NSDictionary *dictionary = [jsonDictionary objectForKey:key];
// get values from the dictionary and set the values for the displayed cell ...

return cell;
}

我尝试为每个文件名和状态值放置一个单元格,在本例中为每个文件名放置 3 个单元格。但我不知道如何做到这一点或获取 FileName 的计数。

这是 XML

<multimedia>
<uploads>
<upload>
<id>1136</id>
<Date>9/9/2016 11:01:58 AM</Date>
<FileName>IMG_0510.MOV</FileName>
<status>2</status>
<Publicacion>Example</Publicacion>
<Seccion>Sistemas</Seccion>
<Event>Q</Event>
</upload>
</uploads>
</multimedia>

最佳答案

在我看来,计数应该是 1。

原始 jsonDictionary 只有一项。您应该更深入地了解上传,然后调用计数。

key @"upload"里面有数组。所以,jsonDictionary[@"multimedia"][@"uploads"][@"upload"]应该返回 array .如果将其转换为 NSDictionary它会返回错误的结果,如果您尝试获取键 File name 的值,您将得到错误的结果。 .

字典的主要目的是让特定的键只有一个值。这就是为什么你只得到一个 File name ,如果将数组转换为 NSDictionary .

所以:

NSArray * array = _xmlDictionary[@"multimedia"][@"uploads"][@"upload"];
NSDictionary * onUpload = array[i];
NSDictionary * fileName = array[i][@"FileName"];
NSString * fileNameString = array[i][@"FileName"][@"text"];

所以在你的情况下:

tableData = jsonDictionary[@"multimedia"][@"uploads"][@"upload"];

然后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
NSDictionary *upload = [tableData objectAtIndex:indexPath.row];
NSString *fileName = upload[@"FileName"][@"text"];
NSString *status = upload[@"status"][@"text"];
NSString *event = upload[@"event"][@"text"];
...

return cell;
}

关于ios - 将 NSDictionary 转换为 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39666552/

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