-
1新建如下图所示
DataManager.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface DataManager : NSObject
+(NSArray*)allDatas;
//从plist文件中获取数据
+(NSArray*)allDatasForPlist:(NSString*)plistName;
//从网络获取数据
+(NSArray*)allDatasForNetURL:(NSString*)url;
@end
NS_ASSUME_NONNULL_END
DataManager.m
#import "DataManager.h"
#import "TRStudent.h"
@implementation DataManager
+(NSArray*)allDatas
{
return [DataManager allDatasForPlist:@"TRStudent"];
}
+(NSArray*)allDatasForPlist:(NSString*)plistName
{ //单独有个 解析类
NSMutableArray *mArray = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle]pathForResource:plistName ofType:@"plist"];
NSArray *students = [NSArray arrayWithContentsOfFile:path];
NSLog(@"%@",students);
for (NSDictionary *dic in students) {
//从字典取出数据 创建对象
TRStudent *stu = [[TRStudent alloc]init];
stu.name = dic[@"name"];
stu.age = [dic[@"age"]integerValue];
//将创建好的学生对象添加到数组中
[mArray addObject:stu];
}
return mArray;
}
//从网络获取数据
+(NSArray*)allDatasForNetURL:(NSString*)url
{ //单独 网络 功能
return nil;
}
@end
TRStudent.h建了与plist文件相同的两个属性没有创建其他方法和事件
TRStudentTableViewController.m
#import "TRStudentTableViewController.h"
#import "DataManager.h"
#import "TRStudent.h"
@interface TRStudentTableViewController ()
@property(nonatomic,strong)NSArray *array;
@end
@implementation TRStudentTableViewController
-(NSArray *)array{
if(!_array){
_array = [DataManager allDatas];
}
return _array;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
@"reuseIdentifier"];
if(!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:@"reuseIdentifier"];
}
TRStudent *stu = self.array[indexPath.row];
cell.textLabel.text = stu.name;
cell.detailTextLabel.text = [NSString
stringWithFormat:@"%ld",stu.age];
return cell;
}
练习新闻列表
下述练习提到了KVC和用xib创建表头存在的bug即处理方式
记住图片的显示方法cell.newsImageView.image = [UIImage imageNamed:newslist.newsImage];
AppDelegate.m设置带导航的视图控制器
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen
mainScreen].bounds];
self.window.rootViewController = [[UINavigationController
alloc]initWithRootViewController:
[[NewListTableViewController alloc]init]];
[self.window makeKeyAndVisible];
return YES;
}
DataManager.m
#import "DataManager.h"
#import "NewsList.h"
@implementation DataManager
+(NSArray *)allDatas
{
return [DataManager allDatasForPlist:@"news"];
}
+(NSArray *)allDatasForPlist:(NSString *)plistName
{
NSMutableArray *mArray = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle]pathForResource:plistName ofType:@"plist"];
NSArray *news = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dic in news) {
NewsList *newlist = [[NewsList alloc]init];
//KVC
//是把字典中的key取出,查找相应的属性以后,用字典中的Value给属性赋值
[newlist setValuesForKeysWithDictionary:dic];
// newlist.title = dic[@"title"];
// newlist.newsImage = dic[@"newsImage"];
// newlist.commentCount = [dic[@"commentCount"]integerValue];
[mArray addObject:newlist];
}
return mArray;
}
创建带.xibcell文件
创建自定义表头
NewListTableViewController.m
#import "NewListTableViewController.h"
#import "NewsList.h"
#import "DataManager.h"
#import "NewsCell.h"
@interface NewListTableViewController ()
@property(nonatomic,strong)NSArray *array;
@end
@implementation NewListTableViewController
-(NSArray*)array{
if(!_array){
_array = [DataManager allDatas];
}
return _array;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.tableHeaderView = [[NSBundle mainBundle]
loadNibNamed:@"Header" owner:nil options:nil].firstObject;
self.tableView.rowHeight = 60;
UIView *headerView = [[NSBundle mainBundle]loadNibNamed:
@"Header" owner:nil options:nil].lastObject;
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake
(0, 0, headerView.frame.size.width,
headerView.frame.size.height)];
[myView addSubview:headerView];
//如果表视图控制器没有用xib 那么 它的表头视图也不用xib,
不过我们可以把xib创建的视图,包在一个没有xib的表头视图中使用这样
就不会存在较大的缝隙
self.tableView.tableHeaderView = myView;
}
#pragma mark - Table view data source
- (NSInteger)
numberOfSectionsInTableView:(UITableView
*)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NewsCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"NewsCell"];
if(!cell){
cell = [[NSBundle mainBundle]loadNibNamed:@"NewsCell"
owner:nil options:nil].firstObject;
}
NewsList *newslist = self.array[indexPath.row];
cell.newsTitle.text = newslist.title;
cell.newsImageView.image = [UIImage
imageNamed:newslist.newsImage];
cell.newsComment.text = [NSString stringWithFormat
:@"%ld",newslist.commentCount];
return cell;
}
结果展示