Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. @interface MainTableViewController :
  2. UITableViewController<LokalModelProtocol>
  3.  
  4.  
  5. @property (strong,nonatomic) NSMutableArray* arr;
  6.  
  7. @end
  8.  
  9. @implementation MainTableViewController
  10. @synthesize arr;
  11.  
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. arr = [[NSMutableArray alloc]init];
  15. LokalModel *lokal = [[LokalModel alloc]init];
  16. lokal.delegate=self;
  17. [lokal downloadItems];
  18.  
  19. }
  20.  
  21. -(void)itemsDownloaded:(NSMutableArray *)items
  22. {
  23. arr=items;
  24. //NSLog(@"%@", items);
  25. [self.tableView reloadData];
  26. }
  27.  
  28. #pragma mark - Table view data source
  29.  
  30. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  31. #warning Incomplete implementation, return the number of sections
  32. return 1;
  33. }
  34.  
  35. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
  36. #warning Incomplete implementation, return the number of rows
  37. // return 1;
  38.  
  39. return [arr count];
  40. }
  41.  
  42.  
  43. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  44. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mainCell" forIndexPath:indexPath];
  45.  
  46. PostModel *post = [[PostModel alloc]init];
  47. post =[arr objectAtIndex:indexPath.row];
  48.  
  49.  
  50. NSLog(@"%@", post.postTitle); ////this outputs the correct strings///////
  51.  
  52. cell.textLabel.text =[NSString stringWithFormat:@"%@", post.postTitle];
  53. cell.detailTextLabel.text = post.postTitle;///neither of these do//////
  54.  
  55. return cell;
  56. }
  57.  
  58. @end
  59.  
  60.  
  61.  
  62.  
  63.  
  64. @protocol LokalModelProtocol <NSObject,NSURLSessionDelegate>
  65.  
  66. +(void)itemsDownloaded:(NSMutableArray*)items;
  67.  
  68. @end
  69.  
  70. @interface LokalModel : NSObject
  71.  
  72. -(void)downloadItems;
  73. @property (strong, nonatomic) NSMutableData* thedata;
  74. @property (strong, nonatomic) NSString* urlString;
  75. @property (strong, nonatomic) NSURL* theUrl;
  76. @property (strong,nonatomic) id<LokalModelProtocol>delegate;
  77. +(void)parseJson:(NSData*)data;
  78.  
  79. @end
  80.  
  81.  
  82.  
  83.  
  84.  
  85. id<LokalModelProtocol>delegate;
  86.  
  87. @implementation LokalModel;
  88. @synthesize thedata,urlString,theUrl,delegate;
  89.  
  90. -(void)downloadItems{
  91. NSURL *theUrl = nil;
  92. static NSString* urlString = @"https://balalatet.com/wp-json/wp/v2/posts";
  93. theUrl=[NSURL URLWithString:urlString];
  94.  
  95. NSURLSession *currentSession= [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  96. NSURLSessionDataTask *task = [currentSession dataTaskWithURL:theUrl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  97. if (error){
  98. [NSException raise:@"error" format:@"%@",error.localizedDescription];
  99. NSLog(@"error1");
  100. }
  101. else{
  102. NSLog(@"success");
  103. [LokalModel parseJson:data];
  104.  
  105. }
  106.  
  107.  
  108. }];
  109. [task resume];
  110.  
  111. }
  112.  
  113. +(void)parseJson:(NSData*)data{
  114.  
  115. NSArray *jsonResults = [[NSArray alloc]init];
  116. NSError *jsonerror;
  117. jsonResults =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonerror];
  118.  
  119. if (jsonerror)
  120. [NSException raise:@"json error" format:@"%@",jsonerror.localizedDescription];
  121.  
  122. NSMutableArray *posts = [[NSMutableArray alloc] init];
  123. NSMutableDictionary *jsonElenent =[NSMutableDictionary dictionary];
  124.  
  125. for (NSMutableDictionary *d in jsonResults)
  126. {
  127. jsonElenent=d;
  128. PostModel *thePost=[[PostModel alloc]init];
  129. thePost.postId= jsonElenent[@"id"];
  130. thePost.postDate= jsonElenent[@"date"];
  131. thePost.postDategmt= jsonElenent[@"date_gmt"];
  132. thePost.postGuid= jsonElenent[@"guid"];
  133. thePost.postSlug= jsonElenent[@"slug"];
  134. thePost.postStatus= jsonElenent[@"status"];
  135. thePost.postSticky= jsonElenent[@"sticky"];
  136. thePost.postPingStatus= jsonElenent[@"ping_status"];
  137. thePost.postType= jsonElenent[@"type"];
  138. thePost.postCommentStatus= jsonElenent[@"comment_status"];
  139. thePost.postTags= jsonElenent[@"tags"];
  140. thePost.postTitle= jsonElenent[@"title"];
  141. thePost.postTemplate= jsonElenent[@"template"];
  142. thePost.postLink= jsonElenent[@"link"];
  143. thePost.postMeta= jsonElenent[@"meta"];
  144. thePost.postModified= jsonElenent[@"modified"];
  145. thePost.postModifiedgmt= jsonElenent[@"modified_gmt"];
  146. thePost.postFeaturedMedia= jsonElenent[@"featured_media"];
  147. thePost.postFormat= jsonElenent[@"format"];
  148. thePost.postLinks= jsonElenent[@"links"];
  149. thePost.postAuthor= jsonElenent[@"author"];
  150. thePost.postContent= jsonElenent[@"content"];
  151. thePost.postCategory= jsonElenent[@"category"];
  152. thePost.postExcerpt= jsonElenent[@"excerpt"];
  153. NSLog(@"%@", thePost.postTitle);
  154. [posts addObject:thePost];
  155. }
  156. dispatch_async(dispatch_get_main_queue(), ^{
  157. [delegate itemsDownloaded:posts];
  158. });
  159. }
  160. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement