Cyph3r Cyph3r - 4 months ago 33
iOS Question

UITableView Array through Model Class

I have implemented a

UITableView
in
ViewController1
,and I am passing the TableView cell content through the Array.

_tableData=[[NSMutableArray alloc]initWithObjects:@"1",@"2" ,@"3",@"4",nil];


I have displayed it as

cell.detailTextLabel.text=[_tableData objectAtIndex:indexPath.row];


Now,I have passed the data through statically,I want to implement it through Dynamic way by using Model Class.

How can I alter the Array through Model Class Array...?

Answer

Create a object class From NSObject

@interface TableClass : NSObject

@property(nonatomic,strong)NSString*tittle;

And import modelClass in your ViewController

dispatch_async(dispatch_get_main_queue(), ^{

NSURLSession*session=[NSURLSession sharedSession];

NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=music"

] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@", json);

    NSArray*entryarr=[json objectForKey:@"results"];


    for (NSDictionary*appDict in entryarr) {
        TableClass*tc=[[TableClass alloc]init];


        //setting tittler

        NSString*str=[appDict objectForKey:@"artistName"];
        [tc setTittle:str];

        NSLog(@"artist Name=%@",tc.tittle);







        [_tableArray addObject:tc];

    }


    [self.tableView reloadData];

}]; [dataTask resume];

});

}

TableClass *tableclassModel=[[TableClass alloc]init]; tableclassModel=[_tableArray objectAtIndex:indexPath.row];

cell.textLabel.text=tableclassModel.tittle;