IOS learning and ios learning routes
Model class:
@interface ListModel : NSObject@property (nonatomic, copy)NSString *time;@property (nonatomic, copy)NSString *cname;@property (nonatomic, copy)NSString *summary;@property (nonatomic, copy)NSString *title;@property (nonatomic, copy)NSString *type;- (void)createArray:(NSDictionary *)result dataSource:(NSMutableArray *)dataSource;
VC:
# Import "ViewController. h" # import "ListModel. h" # import "DetailViewController. h" # define URLSTR @ "http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx? Date = 20131129 & startRecord = 1 & len = 5 & udid = 1234567890 & terminalType = Iphone & cid = 213 "@ interface ViewController () <UITableViewDelegate, UITableViewDataSource> @ property (nonatomic, strong) NSMutableArray * dataSource; @ property (nonatomic, strong) UITableView * tableView; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; self. title = @ "news"; _ dataSource = [[NSMutableArray alloc] initWithCa Pacity: 0]; [self. view addSubview: self. tableView]; // create an asynchronous queue to parse json to prevent blocking the main thread dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_async (queue, ^ {[self urlStr] ;}) ;}# pragma mark -- parse JSON-(void) urlStr {NSURL * url = [NSURL URLWithString: URLSTR]; NSURLSession * session = [NSURLSession sharedSession]; NSURLRequest * request = [NSURLRequest requestWithURL: url]; Required * task = [session dataTaskWithRequest: request completionHandler: ^ (NSData * _ Nullable data, NSURLResponse * _ Nullable response, NSError * _ Nullable error) {NSError * error1; // parse json and return the dictionary. The returned dictionary is unicode encoded. NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: & error1] does not affect normal display. listModel * listModel = [[ListModel alloc] ini T]; [listModel createArray: dict dataSource: _ dataSource]; // the data source starts to be empty because of Network and other reasons... when the data source has a value, refresh TabelView dispatch_async (dispatch_get_main_queue (), ^ {[_ tableView reloadData] ;}]; [task resume];} in the main thread. # pragma mark -- UITableViewDataSource-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return _ dataSource. count;}-(UITableViewCell *) tableView :( UITableView *) tabl EView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cell_id = @ "cell"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cell_id]; if (! Cell) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cell_id];} ListModel * listModel = _ dataSource [indexPath. row]; cell. textLabel. text = listModel. title; cell. detailTextLabel. text = listModel. time; return cell ;}# pragma mark -- UITableViewDelegate-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {ListModel * list Model = _ dataSource [indexPath. row]; DetailViewController * detailVC = [[DetailViewController alloc] init]; [self. navigationController pushViewController: detailVC animated: YES]; detailVC. titleStr = listModel. cname; detailVC. contentStr = listModel. summary;} # pragma mark -- getter-(UITableView *) tableView {if (! _ TableView) {_ tableView = [[UITableView alloc] initWithFrame: self. view. frame]; _ tableView. delegate = self; _ tableView. dataSource = self;} return _ tableView ;}
Complete code, download it here