iOS學習,ios學習路線

來源:互聯網
上載者:User

iOS學習,ios學習路線

Model 類:

@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 = @"新聞";        _dataSource = [[NSMutableArray alloc]initWithCapacity:0];    [self.view addSubview:self.tableView];    //建立一個非同步隊列解析 json,防止阻塞主線程    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);    dispatch_async(queue, ^{        [self urlStr];    });}#pragma mark -- 解析 JSON- (void)urlStr{    NSURL *url = [NSURL URLWithString:URLSTR];    NSURLSession *session = [NSURLSession sharedSession];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {                NSError *error1;        //解析 json,返回字典,這裡解析出來是 unicode 編碼,不影響正常顯示        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error1];                ListModel *listModel = [[ListModel alloc]init];        [listModel createArray:dict dataSource:_dataSource];                //資料來源開始是空的,因為網路等原因...等資料來源有值了,在主線程重新整理 TabelView        dispatch_async(dispatch_get_main_queue(), ^{            [_tableView reloadData];        });    }];    [task resume];}#pragma mark -- UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return _dataSource.count;}- (UITableViewCell *)tableView:(UITableView *)tableView 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 *listModel = _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;}

完整代碼,在這裡下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.