这里是已知网址http://api.milltary.app887.com/api/Articles.action?opc=10&npc=0&type=%E6%9C%80%E6%96%B0%E9%B2%9C,当然也可以是抓来的接口。
言归正传:主要实现代码如下:
//解析文件
- (void)configTableData{
//加载一个NSuRL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.milltary.app887.com/api/Articles.action?opc=10&npc=0&type=%E6%9C%80%E6%96%B0%E9%B2%9C"]];
//将请求的url数据放到NSData中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:nil];
//打开document路径
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",documentPath);
//第二步:创建存储字典的plist文件的存储路径
NSString *dicPath = [documentPath stringByAppendingPathComponent:@"dic.plist"];
//第三步:写入
[dic writeToFile:dicPath atomically:YES];
//读取
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
//解析文件
self.allFourArray = [NSMutableArray new];
NSArray *array = readDic.allKeys;
for (NSString *key in array) {
self.dic1 = [dic objectForKey:key];
}
self.arrays = self.dic1[@"list"];
self.array = [NSMutableArray new];
for (NSDictionary *dict in self.arrays) {
[self.array addObject:dict[@"url"]];
FourModel *fourModel = [[FourModel alloc] initWithDictionary:dict];
[self.allFourArray addObject:fourModel];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//点中某一行实现的方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.array[indexPath.row]]];
}
在刚开始的时候遇到bug,是因为存入沙河路径,强制转换成了NSString类型,可是有属性的类型是Number类型。所以读取的时候要用字符串拼接的方法去解决这一问题。 类型前后一定要保持一致!!!!!!!!
从图片可以看出,工程中代码实现图片的时候,使用:
//图片 图片需要在.h中导入框架
[self.imagelink setImageWithURL:[NSURL URLWithString:model.imglink] placeholderImage:nil];
导入的框架是:#import <UIImageView+WebCache.h>