屏幕快照 2018-12-07 19.31.53.png
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;//创建一个表格
@property (nonatomic,strong)NSArray *tableDataArr;// 给表格赋值的数组
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//请求网络数据
[self requestNetWorkDataAndUpdataUI];
}
//请求网络数据
-(void)requestNetWorkDataAndUpdataUI{
//并发异步线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
self->_tableDataArr = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"News" ofType:@"plist"]];
//回到 UI 主线程刷新 UI
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
//行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _tableDataArr.count;
}
//表格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
NSDictionary *cellDic=_tableDataArr[indexPath.row];//传值
cell.textLabel.text=cellDic[@"title"];
cell.textLabel.numberOfLines=0;
cell.detailTextLabel.text=cellDic[@"content"];
cell.detailTextLabel.numberOfLines=0;
return cell;
}
//初始化表格
- (UITableView *)tableView{
if (!_tableView) {
_tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_tableView.delegate=self;
_tableView.dataSource=self;
}
return _tableView;
}
网友评论