做一个可删除的淘宝购物页面

来源:互联网 发布:慕课网java 百度云盘 编辑:程序博客网 时间:2024/04/30 03:39

项目实现的功能:把商品显示到tableView上,并且给商品分商家,比如,有玩具类商家,糕点类商家,小吃类商家等等。点击每一个商品时,也就是tableView上的每一个cell 都会出现一个对勾,再点击一次对勾就消失,对勾是accessoryType属性中的一种形式,点击全选则全部商品都选上对勾,选择反选则原来没有对勾的选上对勾,原来有对勾的去掉对勾,点击删除时,会出现一个警告框,询问是否要删除,当点击确定时,所有选中的商品都被删除,当一个商家的所有商品都被删除时,这个商家的头标题也会消失。




这是plist文件:里面根据个人需要想添加多少商家类和商品都可以




这是页面:商品所在的页面是一个tableViewy页面,可以拖动的






下面是程序:还有两个类做数据模型,比较简单就没上传

Goods类的头文件:

//

//  Goods.h

//  购物车表格删除1

//

//  Created by  gaopeng on 15-1-7.

//  Copyright (c) 2015 jerei. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Goods : NSObject


@property (nonatomic,copy)NSString *icon;

@property (nonatomic,copy)NSString *name;

@property (nonatomic,copy)NSString *details;


-(id)initWith:(NSDictionary *)dic;

+(id)goodWith:(NSDictionary *)dic;


@end


Goods类的m文件:

//

//  Goods.m

//  购物车表格删除1

//

//  Created by  gaopeng on 15-1-7.

//  Copyright (c) 2015 jerei. All rights reserved.

//


#import "Goods.h"


@implementation Goods


-(id)initWith:(NSDictionary *)dic

{

    if (self = [super init])

    {

        self.icon = [dic objectForKey:@"icon"];

        self.name =        self.icon = [dic objectForKey:@"icon"];

        self.name = [dic objectForKey:@"name"];

        self.details = [dic objectForKey:@"details"];

    }

    return self;


}


+(id)goodWith:(NSDictionary *)dic

{

    Goods *good = [[Goods alloc]initWith:dic];

    

    return good;


}

@end


Shop类的头文件:

//

//  Shop.h

//  购物车表格删除1

//

//  Created by  gaopeng on 15-1-7.

//  Copyright (c) 2015 jerei. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Shop : NSObject


@property (nonatomic,copyNSString *storeName;

@property (nonatomic,copyNSArray *storeGood;


+(id)shopWith:(NSDictionary *)dic;

@end


Shop类的m文件:


//

//  Shop.m

//  购物车表格删除1

//

//  Created by  gaopeng on 15-1-7.

//  Copyright (c) 2015 jerei. All rights reserved.

//


#import "Shop.h"


@implementation Shop


+(id)shopWith:(NSDictionary *)dic

{

    Shop *shop = [Shop new];

    shop.storeName = [dic objectForKey:@"shopName"];

    shop.storeGood = [dic objectForKey:@"shopGoods"];

    

    return shop;

}

@end


ViewController类头文件:


//

//  ViewController.h

//  购物车表格删除1

//

//  Created by  gaopeng on 15-1-7.

//  Copyright (c) 2015 jerei. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property (weaknonatomicIBOutlet UITableView *tableView;


@property (weaknonatomicIBOutlet UIBarButtonItem *all;


@property (weaknonatomicIBOutlet UIBarButtonItem *opposite;

@property (weaknonatomicIBOutlet UIBarButtonItem *dele;



- (IBAction)cliDelBtn:(UIBarButtonItem *)sender;


- (IBAction)cliAllBtn:(UIBarButtonItem *)sender;


- (IBAction)cliOppositeBtn:(UIBarButtonItem *)sender;

@end



ViewController类m文件:

//

//  ViewController.m

//  购物车表格删除1

//

//  Created by  gaopeng on 15-1-7.

//  Copyright (c) 2015 jerei. All rights reserved.

//


#import "ViewController.h"

#import "Goods.h"

#import "Shop.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

    //用来存放每一个店铺的全部商品,有几个商家就有数组几个元素,每一个元素都是一个店铺的全部商品

   NSMutableArray *_goodsAry;

    

    //用来存商家

   NSMutableArray *_shopAry;

    

    //用来存被打对勾的行的商品对象

   NSMutableArray *_selectAry;

    

    //用来接收plist文件中的数据

   NSArray *_ary;


}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

 

    _goodsAry = [NSMutableArrayarray];

    _selectAry = [NSMutableArrayarray];

    _shopAry = [NSMutableArrayarray];

    

    //取数据

    _ary = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"ShoppingGoodsList"ofType:@"plist"]];


#pragma mark  最重要的部分,一定要理解

//========================================================================

   for (int i=0; i<_ary.count; i++)

    {

       NSMutableArray *ary = [NSMutableArrayarray];

       Shop *shop = [ShopshopWith:_ary[i]];

        [_shopAryaddObject:shop];

       for (int j=0; j<[[_ary[i]objectForKey:@"shopGoods"]count]; j++)

        {

           

           Goods *good = [GoodsgoodWith:[_ary[i]objectForKey:@"shopGoods"][j]];

            [aryaddObject:good];

        }

        [_goodsAryaddObject:ary];

        

    }

    

//========================================================================

    _tableView.dataSource =self;

    _tableView.delegate =self;

}


#pragma mark 分组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return_goodsAry.count;


}


#pragma mark 设置行距

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

{

   return 120;


}


#pragma mark 设置每个分区有几行

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

{


   return [_goodsAry[section]count];


}




#pragma mark  给每一行添加内容

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

{

    

    //tableViewcell的重用

    

   static NSString *idGood =@"goods";

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idGood];

    

    

   if (cell==nil)

    {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:nil];

    }

    

    


   Goods *good = _goodsAry[indexPath.section][indexPath.row];

    

    cell.imageView.image = [UIImageimageNamed:good.icon];

    cell.textLabel.text = good.name;

    cell.detailTextLabel.text = good.details;

    cell.detailTextLabel.numberOfLines =0;

    cell.detailTextLabel.textColor = [UIColorbrownColor];

    

#pragma mark   判断当前行的商品是否在选中数组中,若是则让该行显示对号

    

    //判断是否在数组中,如果是则打对勾。

   if ([_selectArycontainsObject:good])

    {

        cell.accessoryType =UITableViewCellAccessoryCheckmark;

    }

   else

    {

        cell.accessoryType =UITableViewCellAccessoryNone;

        

    }

    

    

   return cell;

}


#pragma mark 设置头标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

   return [_shopAry[section]storeName];


}



#pragma mark 点击每一行触发的事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];


   Goods *good = _goodsAry[indexPath.section][indexPath.row];

    

    //2.判断该商品是否在选中数组中

   if ([_selectArycontainsObject:good])

    {

        [_selectAryremoveObject:good];

    }

   else

    {

        [_selectAryaddObject:good];

        

    }

    

    //3.刷新表格(只会刷新屏幕上看到的行的信息)

    

    //    [tableView reloadData];

    

    [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];


}



#pragma mark  点击警告框中的确定

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSMutableArray *indexAry = [NSMutableArrayarray];

   if (buttonIndex==1)

    {

       for (int i=0; i<_selectAry.count; i++)

        {

          

           for (int j=0; j<_goodsAry.count; j++)

            {

               if ([_goodsAry[j]containsObject:_selectAry[i]])

                {

                   NSIndexPath *path = [NSIndexPathindexPathForRow:[_goodsAry[j]indexOfObject:_selectAry[i]]inSection:j];

                    [indexAryaddObject:path];

                }

            }

            

        }

        

        //2.2在商品数组中删除所选中的全部商品

       for (int i=0; i<_selectAry.count; i++)

        {

            

           for (int j=0; j<_goodsAry.count; j++)

            {

               if ([_goodsAry[j]containsObject:_selectAry[i]])

                {

                 [_goodsAry[j]removeObject:_selectAry[i]];

                }

            }

        }

        

        

        //2.3清空盛放所有选中商品的数组

        [_selectAryremoveAllObjects];

        

        //2.4刷新列表

     [_tableViewdeleteRowsAtIndexPaths:indexAry withRowAnimation:UITableViewRowAnimationRight];

        

       for (int i=0; i<_goodsAry.count; i++)

        {

           if ([_goodsAry[i]count]==0)

            {

                [_goodsAryremoveObject:_goodsAry[i]];

                

               //从数据模型中把商家信息删除

                [_shopAryremoveObjectAtIndex:i];

            }

        }

        

       if ([_goodsArycount]==0)

        {

           _all.enabled =NO;

           _opposite.enabled =NO;

           _dele.enabled =NO;

        }

        

        [_tableViewreloadData];

    

   }

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



#pragma mark 点击删除按钮

- (IBAction)cliDelBtn:(UIBarButtonItem *)sender {

    

   if ([_selectArycount]==0)

    {

       UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"警告"message:@"请选择要删除的商品" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];

         [alertshow];

    }

   else

    {

   UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"删除商品" message:@"确定从购物车中删除所选中打商品?" delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];

    

         [alertshow];

    }

    

}



#pragma mark 点击全选,则所有行都打上对勾

- (IBAction)cliAllBtn:(UIBarButtonItem *)sender {

    

    static BOOL a=NO;

   if (a==NO)

    {

       for (int i=0; i<_goodsAry.count; i++)

        {

           for (int j=0; j<[_goodsAry[i]count]; j++)

            {

                [_selectAryaddObject:_goodsAry[i][j]];

            }

        }

        a=YES;

    }


   else if (a==YES)

    {

        [_selectAryremoveAllObjects];

        a=NO;

    }

    

    [_tableView reloadData];

    

}


#pragma mark 点击反选按钮,则以打钩的行取消打钩,没打钩的行打钩

- (IBAction)cliOppositeBtn:(UIBarButtonItem *)sender {

    


       for (int i=0; i<_goodsAry.count; i++)

        {

           for (int j=0; j<[_goodsAry[i]count]; j++)

            {

               if ([_selectArycontainsObject:_goodsAry[i][j]])

                {

                    [_selectAryremoveObject:_goodsAry[i][j]];

                }

               else

                {

                    [_selectAryaddObject:_goodsAry[i][j]];

                

                }

            }

        }


    [_tableView reloadData];

    


}



@end




0 0