IOS菜鸟的所感所思(九)——收藏视图的创建并对cell的初始化

来源:互联网 发布:如何检查网络是否稳定 编辑:程序博客网 时间:2024/06/08 19:41

目标:当我们点击加号按钮时,会将该歌曲添加到收藏列表中。

创建一个CollectMusicVC的视图控制器,用于控制收藏列表的视图。

步聚:

1.首先获得我们已经将信息添加后的SQLite数据表

2.取出实体Music中数据用于放在本地数组中

3.有了数据之后,我们就可以初始化cell信息了

4.这里可以自定义UITableViewCell,在自定义cell中写好接口,进行数据的初始化

5.可以实现对cell的可编辑,即对cell的删除操作


由于我们会在CollectMusicVC中获取数据,而用户可能会多次切换视图,因此会将获取数据的方法放在

- (void)viewDidAppear:(BOOL)animated中。

//获取数据并进行重新加载

- (void)viewDidAppear:(BOOL)animated{

    [superviewDidAppear:animated];

    

    [selfqueryDataFromSQLite];


    [self.tableViewreloadData];

}


/**

 *  取数据,并存放在本地数组中

 */

- (void)queryDataFromSQLite{

   

   NSError *error =nil;

//会调用DBManager中的接口进行查询数据操作

    self.fetchMusicArray = [DBManagergetDataFromEntity:@"Music"managedObjectContext:self.myAppDelegate.managedObjectContext];

    if (_fetchMusicArray ==nil) {

       NSLog(@"Error:%@",error);

    }

    NSLog(@"The count of Your music:%ld",[_fetchMusicArraycount]);

    

}

因此在DBManager类中的类方法

+ (NSMutableArray *)getDataFromEntity:(NSString *)EntityName managedObjectContext:(NSManagedObjectContext *)context{

    

   NSError *error =nil;

   NSFetchRequest *request = [NSFetchRequestfetchRequestWithEntityName:EntityName];

    //获取实体Music,并取得实体中的数据对象数组

    return [NSMutableArrayarrayWithArray:[contextexecuteFetchRequest:requesterror:&error]];

}

将返回后的数组数组初始化本地数组。

当然在方法调用过程中会用到参数的,

@property (nonatomic,strong)NSMutableArray *fetchMusicArray;

@property (nonatomic,strong)AppDelegate *myAppDelegate;

而变量的初始化在

- (void)viewDidLoad {

    [superviewDidLoad];

    _myAppDelegate = (AppDelegate *)[[UIApplicationsharedApplication]delegate];

}

获得了数据源之后就可以处理视图方面的问题了。

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    

   return1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.

    NSLog(@"count:%ld",[_fetchMusicArraycount]);

    return [_fetchMusicArraycount];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

   return50;

}


接着是自定义cell

创建一个继承UITableViewCell的类CollectViewCell,


并进行相应试图控制器的关联

@interface CollectViewCell()

@property (weak, nonatomic) IBOutletUIImageView *imageLogo;

@property (weak, nonatomic) IBOutletUILabel *songName;

@property (weak, nonatomic) IBOutletUILabel *albumName;


@end

这样自定义算是完成了

接着就是cell的初始化工作了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   staticNSString *cellIdentifier =@"collectMusicCell";

    

   CollectViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];

   if (cell ==nil) {

        cell = [[[NSBundlemainBundle]loadNibNamed:@"CollectViewCell"owner:selfoptions:nil]lastObject];

    }

   Music *music = [_fetchMusicArrayobjectAtIndex:indexPath.row];

    //调用接口的方法

    [cellsetInfo:music];

    

   return cell;

}

当然这个接口方法在CollectViewCell中

- (void)setInfo:(Music *)music{

    self.songName.text = music.trackname;

    self.albumName.text = music.albumname;

    

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

       NSURL *imageUrl = [NSURLURLWithString:music.logoname];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.imageLogosd_setImageWithURL:imageUrlplaceholderImage:[UIImageimageNamed:@"surf.jpg"]];

        });

        

    });

    

}

在这里之前我们说的那句没起作用的代码[item setValue:musicLogoforKey:@"logoname"];在这里起了作用。

这里还是要导入第三方库#import"UIImageView+WebCache.h"

最后的工作就是可编辑的工作了

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle ==UITableViewCellEditingStyleDelete) {

        

        [selfdeleteDataFromSQLite:indexPath];

        

    }

}

- (void)deleteDataFromSQLite:(NSIndexPath *)indexPath{

    

   NSError *error =nil;

    

    if (_fetchMusicArray ==nil) {

       NSLog(@"Error:%@",error);

    }

    //获得该行要编辑的对象

   Music *music = [_fetchMusicArrayobjectAtIndex:indexPath.row];

    //调用DBManager中方法

    [DBManager deleteDataFromSQLite:musicmanagedObjectContext:self.myAppDelegate.managedObjectContext];

    

    [_fetchMusicArrayremoveObjectAtIndex:indexPath.row];

    [self.tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];

    //重新加载

    [self.tableViewreloadData];

}

DBManager中的删除操作:

+ (void)deleteDataFromSQLite:(id)anObject managedObjectContext:(NSManagedObjectContext *)context{

   NSError *error;

    [contextdeleteObject:anObject];

    

   if([contextsave:&error]){

       NSLog(@"Error:%@,%@",error,[erroruserInfo]);

    }

}


到目前为止的完整代码,请点击:
点击打开链接



0 0