1、数据库解析出来的数据:
- {
- "code": "200",
- "data":
- [
- {
- "user_id": "2",
- "diary_id": "3",
- "image_id": "39",
- "user_nickname": "耳朵在跳动",
- "image_path": "h007.png",
- "diary_time": "2015-05-20 14:30:54",
- "image_address": "福建省 武夷山市 武夷山风景区",
- "image_describe": "会当临绝顶,一览众山小",
- "diary_title": "精雕细琢的园林建筑"
- },
- {
- "user_id": "2",
- "diary_id": "6",
- "image_id": "49",
- "user_nickname": "耳朵在跳动",
- "image_path": "h009.png",
- "diary_time": "2015-04-20 14:30:54",
- "image_address": "福建省 厦门市 鼓浪屿",
- "image_describe": "欧洲风格的小房子",
- "diary_title": "乃是一大堆假山石"
- },
- {
- "user_id": "2",
- "diary_id": "2",
- "image_id": "44",
- "user_nickname": "耳朵在跳动",
- "image_path": "h005.png",
- "diary_time": "2015-01-20 14:30:54",
- "image_address": "中国 台湾省 垦丁",
- "image_describe": "美丽的垦丁",
- "diary_title": "桃花坞里桃花庵"
- }
- ]
- }
2、客户端解析数据库:(用AFNetWork框架,发送post请求,用code=200表示解析成功,解析出来的数据中,data里面放的是一个数组,数组里面的数据又是一个个字典,用一个临时变量arr来接收数组数据,然后用一个for in循环来遍历出arr中的三个大的字典,然后将三个字典数据封装成一个TDCollect的model,在model中有一个实例化的方法,可以来获取每个大字典中的九个小的字典数据,然后在将这三个字典存放在一个全局的数组collectArray等待读取)
在一个tableview的.m文件中
-(void)sendRequestCollect{
page++;
NSString *URL = KHTTPCollect;
NSDictionary *parameter = @{
@"user_id":@(APPDELEGATE.user.user_id),
@"page":[NSNumber numberWithInteger:page]
};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:URL parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dicAll = (NSDictionary *)responseObject;
NSString *code = dicAll[@"code"];
if ([code isEqualToString:@"200"]) {
NSMutableArray *arr = [NSMutableArray array];
arr = dicAll[@"data"];
for (NSDictionary *obj in arr) {
TDCollect *tdcollect = [TDCollect initWithCollectDic:obj];
[collectArray addObject:tdcollect];
}
//分页请求
[self.tableView reloadData];
[self.tableView.footer endRefreshing];
}else{
[self.tableView.footer endRefreshing];
[self.tableView.footer noticeNoMoreData];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
3、TDCollect模板
(1).h文件
@property(copy,nonatomic)NSString *userId;
@property(copy,nonatomic)NSString *diaryId;
@property(copy,nonatomic)NSString *imageId;
@property(copy,nonatomic)NSString *diaryTitle;
@property(copy,nonatomic)NSString *nickname;
@property(copy,nonatomic)NSString *diaryTime;
@property(copy,nonatomic)NSString *imageAddress;
@property(copy,nonatomic)NSString *imageDescribe;
@property(copy,nonatomic)NSString *imagePath;
-(TDCollect *)initWithCollectUserId:(NSString *)userId andDiaryId:(NSString *)diaryId andImageId:(NSString *)imageId andDiaryTime:(NSString *)diaryTime andImageAddress:(NSString *)imageAddress andImageDescribe:(NSString *)imageDescribe andNickname:(NSString *)nickname andDiaryTitle:(NSString *)diaryTitle andImagePath:(NSString *)imagePath;
+(TDCollect *)initWithCollectDic:(NSDictionary *)dic;
(2).m文件
-(TDCollect *)initWithCollectUserId:(NSString *)userId andDiaryId:(NSString *)diaryId andImageId:(NSString *)imageId andDiaryTime:(NSString *)diaryTime andImageAddress:(NSString *)imageAddress andImageDescribe:(NSString *)imageDescribe andNickname:(NSString *)nickname andDiaryTitle:(NSString *)diaryTitle andImagePath:(NSString *)imagePath{
if (self = [super init]) {
self.userId = userId;
self.diaryId = diaryId;
self.imageId = imageId;
self.diaryTime = diaryTime;
self.imageAddress = imageAddress;
self.imageDescribe = imageDescribe;
self.nickname = nickname;
self.diaryTitle = diaryTitle;
self.imagePath = imagePath;
}
return self;
}
+(TDCollect *)initWithCollectDic:(NSDictionary *)dic{
TDCollect *tdcollect = [[TDCollect alloc]initWithCollectUserId:dic[@"user_id"] andDiaryId:dic[@"diary_id"] andImageId:dic[@"image_id"] andDiaryTime:dic[@"diary_time"] andImageAddress:dic[@"image_address"] andImageDescribe:dic[@"image_describe"] andNickname:dic[@"user_nickname"] andDiaryTitle:dic[@"diary_title"] andImagePath:dic[@"image_path"]];
return tdcollect;
}
4.还是在tableview的.m文件(自定义一个cell,来显示没一个cell中的数据样式)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TDCollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TDCollectTableViewCell" forIndexPath:indexPath];
TDCollect *tdcollect = collectArray[indexPath.row];
[cell setUpWithCollect:tdcollect];
return cell;
}
5.在tableviewcell 的.m文件中
-(void)setUpWithCollect:(TDCollect *)tdcollect{
self.positionLable.text = tdcollect.imageAddress;
self.nicknameLabel.text = tdcollect.nickname;
self.titleLable.text = tdcollect.diaryTitle;
self.descripeLabel.text = tdcollect.imageDescribe;
self.diaryTimeLabel.text = tdcollect.diaryTime;
NSString *imagePath = [NSString stringWithFormat:@"%@%@",kHTTPDiaryImg,tdcollect.imagePath];
NSURL *URL = [NSURL URLWithString:imagePath];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.coverImage setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
//显示图片
self.coverImage.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
}];
}