IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    ios练手小项目之物品删除

    键盘上的舞者发表于 2015-05-25 19:48:31
    love 0

    效果如图所示,没有选中物品的时候,垃圾桶的图表是灰色的,选中的时候是激活状态。

    选择一个物品,右边出现勾勾,上面的标签显示要删除多少条,删除的时候有弹窗进行确认。

    代码如下:

    //
    //  ViewController.m
    //  淘宝物品删除
    //
    //  Created by 键盘上的舞者 on 15/5/26.
    //  Copyright (c) 2015年 键盘上的舞者. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Shop.h"
    @interface ViewController ()
    {
        NSMutableArray *_shops;
        NSMutableArray *_deshops;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //从shops.plist中加载数据
        NSString *path=[[NSBundle mainBundle] pathForResource:@"淘宝/shops" ofType:@"plist"];
        NSArray *array=[NSArray arrayWithContentsOfFile:path];
        _shops=[NSMutableArray array];
        _deshops=[NSMutableArray array];
        //将array中的字典对象转成Shop模型对象
        for (NSDictionary *dict in array) {
            Shop *s=[[Shop alloc] initWithDict:dict];
            [_shops addObject:s];
        }
       
    }
    
    //实现删除的功能
    -(void)remove
    {
        NSString *warning=[NSString stringWithFormat:@"你确定要删除这%d项吗?",_deshops.count];
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:warning message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.alertViewStyle=UIAlertViewStyleDefault;
        [alert show];
    }
    //UIAlertView的代理方法
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex==0)return;
        [self shanchu];
    }
    -(void)shanchu
    {
        for (NSArray *array in _deshops) {
            [_shops removeObject:array];
        }
        _del.title=@"删除";
        _delItem.enabled=NO;
        [_deshops removeAllObjects];
        [_tableView reloadData];
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _shops.count;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *ID=@"cc";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
        if(cell==nil)
        {
            cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        }
        //设置cell的属性
        Shop *s=_shops[indexPath.row];
        cell.textLabel.text=s.name;
        cell.detailTextLabel.text=s.desc;
        NSString *imgName=[NSString stringWithFormat:@"淘宝/%@",s.icon];
        cell.imageView.image=[UIImage imageNamed:imgName];
        if([_deshops containsObject:s])
        {
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }else{
            cell.accessoryType=UITableViewCellAccessoryNone;
        }
        return cell;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 80;
    }
    //当前行没有被选择的时候
    -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
    {
       
    }
    //当前行被选择的时候
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        //取消当前行的高亮状态
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
       
        UITableViewCell *selected=[tableView cellForRowAtIndexPath:indexPath];
      //  selected.accessoryType=UITableViewCellAccessoryCheckmark;
        Shop *s=_shops[indexPath.row];
        if(selected.accessoryType==UITableViewCellAccessoryCheckmark)
        {
            [_deshops removeObject:s];
        }else{
            [_deshops addObject:s];
        }
        if(_deshops.count==0)
        {
            _del.title=@"删除";
            _delItem.enabled=NO;
        }else{
            NSString *d=[NSString stringWithFormat:@"删除(%d)",_deshops.count];
            _del.title=d;
            _delItem.enabled=YES;
        }
    //    //刷新
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
       // [tableView reloadData];
    }
    @end



沪ICP备19023445号-2号
友情链接