创建Model
@interface Model : NS
@property (nonatomic,strong)NSString *imgsrc,* ;
@end
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
创建DataManager
#import <Foundation/Foundation.h>
#import “Student+CoreDataClass.h”
#import “Model.h”
NS_ASSUME_NONNULL_BEGIN
@interface DataManager : NS
+(DataManager *)shareManager;
@property (nonatomic,strong)NSString *imgsrc,* ;
-(void)insert:(NSDictionary *)dic;
-(void)update:(Student *)student;
-(void)deleteData:(Model *)student;
-(NSArray *)select;
@end
NS_ASSUME_NONNULL_END
#import “DataManager.h”
#import “AppDelegate.h”
#import “Model.h”
@interface DataManager(){
//声明一个APPdelegate对象属性,调用里面的被管理对象上下文 保存方法
AppDelegate *myDelegate;
}
@end
@implementation DataManager
+(DataManager *)shareManager{
static DataManager *manager;
if (!manager) {
manager = [[DataManager alloc] init];
}
return manager;
}
-(void)insert:(NSDictionary *)dic{
//CoreData 数据的插入
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//创建实体描述对象
NSEntityDe ion *de ion = [NSEntityDe ion entityForName:@“Student” inManaged Context:myDelegate.persistentContainer.viewContext];
//创建一个模型对象
Student *student = [[Student alloc] initWithEntity:de ion insertIntoManaged Context:myDelegate.persistentContainer.viewContext];
student. = dic[@“ Lab”];
student.imgsrc = dic[@“img”];
//对数据管理器的更改进行永久保存
[myDelegate saveContext];
}
//-(void)update:(Student *)student{
// myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// [myDelegate saveContext];
//}
-(NSArray *)select{
myDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//查询数据
//1.创建查询请求
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@“Student”];
//2.执行查询请求
NSError *error = nil;
NSArray *result = [myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
NSMutableArray *muarry = [[NSMutableArray alloc] init];
for (Student *std in result) {
Model *model = [Model new];
model. = std. ;
model.imgsrc = std.imgsrc;
[muarry add :model];
}
return [muarry copy];
}
-(void)deleteData:(Model *)student{
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@“Student”];
//2.执行查询请求
NSError *error = nil;
NSArray *result = [myDelegate.persistentContainer.viewContext executeFetchRequest:request error:&error];
for (Student *std in result) {
if ([std. isEqualToString:student. ]&&[std.imgsrc isEqualToString:student.imgsrc]) {
[myDelegate.persistentContainer.viewContext delete :std];
[myDelegate saveContext];
}
}
}
@end
创建view 自定义表格
@interface ZDYTableViewCell : UITableViewCell
@property (nonatomic,strong)UILabel * Lab;
@property (nonatomic,strong)UIImageView *img;
-
(void)loadData:(Model *)mod;
-
(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.img=[[UIImageView alloc]initWith :CGRectMake(10, 0, 50, 50)];
self.img.backgroundColor = [UIColor lightGrayColor];
self. Lab=[[UILabel alloc]initWith :CGRectMake(70, 0, 300, 50)];
// self. Lab.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.img];[self.contentView addSubview:self. Lab]; [self set ];}
return self;
}
-(void)set {
}
- (void)loadData:(Model *)mod{
if (mod) {
self. Lab.text=mod. ;
self.img.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:mod.imgsrc]]];
}
}
@end
创建控制器ViewController 我这个在系统写的
#import “ViewController.h”
#import “AFNetworking.h”
#import “Model.h”
#import “ZDYTableViewCell.h”
#import “DataManager.h”
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *table;
@property (nonatomic,strong)NSMutableArray *data;
@end
static NSString *oj = @“cell”;
@implementation ViewController
-
(void)viewDidLoad {
[super viewDidLoad];[self.table registerClass:[ZDYTableViewCell class] forCellReuseIdentifier:oj];
self.data=[[NSMutableArray alloc]init];
[self CreateAFNet];
[self.view addSubview:self.table];
[self.data add sFromArray: [[DataManager shareManager] select]];
} -
(UITableView *)table{
if (!_table) {
_table=[[UITableView alloc]initWith :self.view. ];
_table.delegate=self;
_table.dataSource=self;
}return _table;
} -
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.data.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ZDYTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:oj];
if (self.data.count>0) {
DataManager *mod=self.data[indexPath.row];
[cell loadData:mod];}
return cell;
} -
(void)CreateAFNet{
// return;
AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
[manager GET:@“http://c.m.163.com/nc/article/headline/T1348647853363/0-20.html” parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable response ) {
NSLog(@\"%@\",response );for ( NSDictionary *dic in response [@\"T1348647853363\"]) { Model *mod=[[Model alloc]init]; [mod setValuesForKeysWithDictionary:dic]; [self.data add :mod]; [[DataManager shareManager] insert:@{@\" Lab\":mod. ,@\"img\":mod.imgsrc}];
}
[self.table reloadData];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@\"%@\",error);
}];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除数据库的
[[DataManager shareManager] deleteData:self.data[indexPath.row]];
//删除数据源
[self.data remove :self.data[indexPath.row]];
[self.table reloadData];
}
}
@end
继续阅读与本文标签相同的文章
-
阿里云910会员节:开启时光机领会员大礼包,云产品满减优惠活动
2026-05-18栏目: 教程
-
基于宜搭的“报表分析”实践案例
2026-05-18栏目: 教程
-
javascript教程:实现函数柯里化与反柯里化
2026-05-18栏目: 教程
-
基于宜搭的“定时消息通知”实践案例
2026-05-18栏目: 教程
-
AIoT入门:用虚拟设备体验物联网平台设备上云&设备数据存储
2026-05-18栏目: 教程
