我有一个应用程序,其中有一个响应,如:
{
"16-06-2016" = (
{
cccc = 16;
dddd = 17;
}
);
"17-06-2016" = (
{
cccc = 14;
dddd = 19;
},
{
cccc = 1;
dddd = 9;
}
);
"18-06-2016" = (
{
cccc = 14;
dddd = 19;
},
{
cccc = 1;
dddd = 9;
}
);
}
如何在NSDictionary
中显示此UITableView
。有人能帮助我吗?
答案 0 :(得分:2)
尝试这样的事情
self.sectionArr = [jsonDic allKeys]; //First get all section from dictionary like this
现在实施TableView
委托方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.sectionArr.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.sectionArr objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *arr = [jsonDic objectForKey:[self.sectionArr objectAtIndex:section]];
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell= [tableview dequeueReusableCellWithIdentifier:@"Cell"];
NSArray *arr = [jsonDic objectForKey:[self.sectionArr objectAtIndex:indexPath.section]];
cell.textLabel.text = [[arr objectAtIndex:indexPath.row] valueForKey:@"dddd"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *arr = [jsonDic objectForKey:[self.sectionArr objectAtIndex:indexPath.section]];
NSLog(@"Selected Obj - %@",[[arr objectAtIndex:indexPath.row] valueForKey:@"dddd"]);
}
希望这会对你有所帮助。
答案 1 :(得分:0)
当我在UITableView中显示NSDictionary时,由于像cellForRowAtIndexPath
之类的委托方法使用索引,我通常会这样做以获取特定索引处的项目:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get array of keys
NSArray *keys = [self.dictionary allKeys];
// Get key for the current row
NString *key = [keys objectAtIndex:indexPath.row];
// Get object for current row
id object = [self.dictionary objectForKey:key];
// Then do the typical stuff
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:@"Cell"
forIndexPath:indexPath];
....
虽然,在你的情况下,"对象"是一个NSDictionary,所以做这样的事情:
...
// Get object for current row
NSDictionary *item = [self.dictionary objectForKey:key];
id cccc = [item objectForKey:@"cccc"];
id dddd = [item objectForKey:@"dddd"];
....
如果您需要对键进行排序,请使用keysSortedByValueUsingSelector:
而不是allKeys
。
答案 2 :(得分:0)
也许是这样的?
(我把你提供的数据放入 dict 变量)
@interface MainViewController () {
NSDictionary *dict;
NSArray *sortedKeysAsSectionNames;
}
- (void)viewDidLoad {
[super viewDidLoad];
dict = @{@"16-06-2016": @[@{@"cccc": @(16), @"dddd": @(17)}],
@"17-06-2016": @[@{@"cccc": @(14), @"dddd": @(19)},
@{@"cccc": @(1), @"dddd": @(9)}],
@"18-06-2016": @[@{@"cccc": @(14), @"dddd": @(19)},
@{@"cccc": @(1), @"dddd": @(9)}]
};
NSArray *dictKeys = [dict allKeys];
sortedKeysAsSectionNames = [dictKeys sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"dd-MM-yyyy";
NSDate *date1 = [format dateFromString:(NSString *)obj1];
NSDate *date2 = [format dateFromString:(NSString *)obj2];
return [date1 compare:date2];
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sortedKeysAsSectionNames.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [(NSArray *)[dict objectForKey:sortedKeysAsSectionNames[section]] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
id key = sortedKeysAsSectionNames[indexPath.section];
NSArray *dataInSectionArray = [dict objectForKey:key];
NSDictionary *dataForCellDict = [dataInSectionArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = [[dataForCellDict objectForKey:@"cccc"] description];
cell.detailTextLabel.text = [[dataForCellDict objectForKey:@"dddd"] description];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return sortedKeysAsSectionNames[section];
}