-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMTableViewController.m
84 lines (53 loc) · 2.38 KB
/
TMTableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// TMTableViewController.m
// JSONapp
//
// Created by Тарас on 23.11.16.
// Copyright © 2016 Taras Minin. All rights reserved.
//
#import "TMTableViewController.h"
@interface TMTableViewController ()
@property (strong, nonatomic) NSDictionary *cellInfo;
@property (strong, nonatomic) NSArray *cellText;
@end
@implementation TMTableViewController
- (id) initWithInfo:(NSDictionary*) info {
self = [super init];
_cellInfo = info;
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *allKeys = [[NSArray alloc] initWithArray:[_cellInfo allKeys]];
_cellText = [allKeys sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_cellText count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
NSString *option = [NSString stringWithFormat:@"%@", [_cellText objectAtIndex:indexPath.row]];
NSString *detail = [NSString stringWithFormat:@"%@", [_cellInfo valueForKey:[_cellText objectAtIndex:indexPath.row]]];
if ([detail hasPrefix:@"{"]) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else {
NSString *detailClean = [detail stringByReplacingOccurrencesOfString:@"\n" withString:@""];
cell.detailTextLabel.text = detailClean;
}
cell.textLabel.text = option;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryDisclosureIndicator) {
NSDictionary *dict = [[NSDictionary alloc] initWithDictionary:[_cellInfo valueForKey:[_cellText objectAtIndex:indexPath.row]]];
TMTableViewController *newController = [[TMTableViewController alloc] initWithInfo:dict];
newController.title = selectedCell.textLabel.text;
[self showViewController:newController sender:nil];
}
}
@end