码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-自定义cell的方法步骤

时间:2015-08-08 09:11:42      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:



#import "ViewController.h"

#import "MyTableViewCell.h"

#import "AddData_ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    NSMutableArray *list;

    UITableView *myTabelView;

    NSString *path;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];

    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];

    

    

    [self creatTableView];

    

    

    

    

}


- (void)edit

{

    [myTabelView setEditing:!myTabelView.isEditing animated:YES];

}


- (void)add

{

    AddData_ViewController *addVc = [[AddData_ViewController alloc]init];

    [self.navigationController pushViewController:addVc animated:YES];

}


//编辑tableViewcell

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

{

    //盘点编辑中的样式 是否是删除的样式

    if (editingStyle == UITableViewCellEditingStyleDelete) {

     

        //1.删除数据

        //2.更新视图

        

       [list removeObjectAtIndex:indexPath.row];

        

        //把移除后的数据同步到plist里面

       BOOL success = [list writeToFile:path atomically:YES];

        

        //如果数据移除成功就删除cell

        if (success) {

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

        }

    

        

        

    }

}


//设置是否可以编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}



- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

//    如果移动的不是同一个位置

    if (sourceIndexPath != destinationIndexPath) {

        

//        1.先保存需要移动的数据;sourceIndexPath.row需要移动的数据

        NSDictionary *info = list[sourceIndexPath.row];

        

//        2.移除需要移动的数据

        [list removeObjectAtIndex:sourceIndexPath.row];

        

//        3.插入数据 destinationIndexPath 可以得到需要移动的位置

          [list insertObject:info atIndex:destinationIndexPath.row];

        

//        同步数据到plist文件

        [list writeToFile:path atomically:YES];

        

        

        

    }

}



- (void)loadData

{

    path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Detail_Data_ List.plist"];

    list = [[NSArray arrayWithContentsOfFile:path] mutableCopy];

    

    NSLog( @"%@",list);

    

    if (myTabelView) {

        

//        reloadData必须在主线程 刷新UI

        [myTabelView reloadData];

    }

};



- (void)viewWillAppear:(BOOL)animated

{

    [self loadData];

}



- (void)creatTableView

{

    myTabelView = [[UITableView alloc]initWithFrame: self.view.frame style:UITableViewStylePlain];

    myTabelView.delegate = self;

    myTabelView.dataSource = self;

    [self.view addSubview:myTabelView];

}



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

{

    return list.count;

}



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

{

    NSString *vigetableIndentifire = @"cellID";

    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vigetableIndentifire];

    if (!cell) {

        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vigetableIndentifire];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.showImage.image = [UIImage imageNamed:list[indexPath.row][@"ImageName"]];

    cell.nameLable.text = [NSString stringWithFormat:@"名字:%@ ",list[indexPath.row][@"name"]];

    cell.priceLable .text = [NSString stringWithFormat:@"价格: %@/",list[indexPath.row][@"price"]];

    cell.contentLable.text = [NSString stringWithFormat:@"介绍:%@",list[indexPath.row][@"content"]];

    return cell;

}




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

{

    return 200;

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS-自定义cell的方法步骤

标签:

原文地址:http://blog.csdn.net/jzq_sir/article/details/47355011

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!