Plist
然后利用Plist作为数据存储,实现类似的qq界面,使用UITableViewController
该项目充分利用MVC框架结构
//
// DataOperation.h
// QQ
//
// Created by PXD on 15-4-28.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DataOperation : NSObject
//加载数据
+ (id)loadData;
//解析数据
+ (id)dealWithData:(id)data;
@end
//
// DataOperation.m
// QQ
//
// Created by PXD on 15-4-28.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "DataOperation.h"
#import "FriendsModel.h"
@implementation DataOperation
//从plist文件读取数据
+ (id)loadFromPlist{
//获取文件的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"QQPlist" ofType:@"plist"];
//读取文件的内容 ->nsarray
NSArray *qqsArray = [NSArray arrayWithContentsOfFile:filePath];
//返回数据
return qqsArray;
}
+ (id)loadData{
return [self loadFromPlist];
}
+ (id)dealWithData:(id)data{
NSArray *qqsArray = (NSArray *)data;
//1.创建一个数组 用来保存所有的分组信息
NSMutableArray *containersArray = [NSMutableArray arrayWithCapacity:0];
//2.利用一个循环依次解析每一组
for(NSDictionary *groupDic in qqsArray){
//获取member数组
NSArray *orgMembersArray = [groupDic objectForKey:@"members"];
//提供一个数组保存这一组所有的friendModel
NSMutableArray *modelsArray = [NSMutableArray arrayWithCapacity:0];
//遍历成员数组
for (NSDictionary *eachFriendDic in orgMembersArray){
//创建一个好友的model对象
FriendsModel *model = [[FriendsModel friendModel] initWithData:eachFriendDic];
//保存这个model对象
[modelsArray addObject:model];
}
//将modelsArray 保存到containersArray里面
[containersArray addObject:modelsArray];
}
return containersArray;
}
@end
//
// CellsOperation.h
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CellsOperation : NSObject
+ (id)tableView:(UITableView *)tableView
loadCell:(NSString *)nibName
WithIdentifier:(NSString *)identifier
indexPath:(NSIndexPath *)indexPath;
@end
//
// CellsOperation.m
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "CellsOperation.h"
#import "FriendCell.h"
@implementation CellsOperation
+ (id)tableView:(UITableView *)tableView
loadCell:(NSString *)nibName
WithIdentifier:(NSString *)identifier
indexPath:(NSIndexPath *)indexPath{
if (indexPath != nil) {
//storyboard
return [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
}
if (identifier == nil) {
//xib
return [[[NSBundle mainBundle] loadNibNamed:nibName owner:nil options:nil] lastObject];
}
// if (nibName != nil && identifier != nil) {
// //手动创建
// Class ClassName = NSClassFromString(nibName);
// FriendCell cell = (FriendCell)ClassName;
// cell = [[ClassName alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
// return cell;
// }
return nil;
}
@end
//
// FriendsModel.h
// QQ
//
// Created by PXD on 15-4-28.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface FriendsModel : NSObject
@property (nonatomic, strong) NSString *header;
@property (nonatomic, strong) NSString *friendName;
@property (nonatomic, strong) NSString *mood;
@property (nonatomic, assign) BOOL vip;
+ (instancetype)friendModel;
- (instancetype)initWithData:(NSDictionary *)dic;
@end
//
// FriendsModel.m
// QQ
//
// Created by PXD on 15-4-28.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "FriendsModel.h"
@implementation FriendsModel
+ (instancetype)friendModel{
return [[FriendsModel alloc] init];
}
// //headerImage
// model.header = [eachFriendDic objectForKey:@"header"];
// //name
// model.friendName = [eachFriendDic objectForKey:@"friendName"];
// //mood
// model.mood = [eachFriendDic objectForKey:@"mood"];
// //vip
// model.vip = [eachFriendDic objectForKey:@"vip"];
- (instancetype)initWithData:(NSDictionary *)dic{
[self setValuesForKeysWithDictionary:dic];
return self;
}
@end
//
// FriendCell.h
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "FriendsModel.h"
@interface FriendCell : UITableViewCell
@property (nonatomic, strong) FriendsModel *model;
+ (instancetype)tableView:(UITableView *)tableView
loadCellWithIdentifier:(NSString *)identifier
indexPath:(NSIndexPath *)indexPath;
@end
//
// FriendCell.m
// QQ
//
// Created by PXD on 15-4-29.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "FriendCell.h"
@interface FriendCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *moodLabel;
@end
@implementation FriendCell
+ (instancetype)tableView:(UITableView *)tableView loadCellWithIdentifier:(NSString *)identifier indexPath:(NSIndexPath *)indexPath{
return [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setModel:(FriendsModel *)model{
if (_model != model) {
_model = model;
}
_headerImageView.image = [UIImage imageNamed:_model.header];
_nameLabel.text = _model.friendName;
_moodLabel.text = _model.mood;
}
@end
//
// ViewController.h
// QQ
//
// Created by PXD on 15-4-28.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UITableViewController
@end
//
// ViewController.m
// QQ
//
// Created by PXD on 15-4-28.
// Copyright (c) 2015年 PXD. All rights reserved.
//
#import "ViewController.h"
#import "DataOperation.h"
#import "FriendCell.h"
#import "FriendsModel.h"
@interface ViewController ()
@property (nonatomic, strong) NSArray *qqsArray;
@property (nonatomic, strong) NSMutableArray *friendsModelsArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self loadData];
}
- (void)loadData{
//加载数据
self.qqsArray = [DataOperation loadData];
//解析数据
self.friendsModelsArray = [DataOperation dealWithData:_qqsArray];
//显示到界面上 刷新界面 重新加载tableView
[self.tableView reloadData];
}
#pragma mark -- UITableViewDelegate & DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _friendsModelsArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//取出这个section对应的数组
return ((NSArray *)[_friendsModelsArray objectAtIndex:section]).count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
- (FriendCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//创建cell
FriendCell *cell = [FriendCell tableView:tableView loadCellWithIdentifier:@"friendCellID" indexPath:indexPath];
//获取这个cell对应的model
NSArray *groupArray = [_friendsModelsArray objectAtIndex:indexPath.section];
FriendsModel *model = [groupArray objectAtIndex:indexPath.row];
//设置cell的数据源
cell.model = model;
return cell;
}
@end
上午Plist部分听的没有问题,下午项目部分出现很大问题,好多不懂的地方,主要是有些“大跃进”,速度有点快。
总体还是自己本身能力差劲,还需多加修炼,加油补充。
感觉越难,就在越进步,安慰下自己吧!