做一个可删除的淘宝购物页面
来源:互联网 发布:慕课网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
// 购物车表格删除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.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,copy) NSString *storeName;
@property (nonatomic,copy) NSArray *storeGood;
+(id)shopWith:(NSDictionary *)dic;
@end
//
// 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.h
// 购物车表格删除1
//
// Created by gaopeng on 15-1-7.
// Copyright (c) 2015年 jerei. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *all;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *opposite;
@property (weak, nonatomic) IBOutlet 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
{
//tableView中cell的重用
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
- 做一个可删除的淘宝购物页面
- 做一个可删除的淘宝购物页面
- 桌面淘宝购物 删除
- 在页面做一个可拖动的进度条
- 安卓仿淘宝下拉加载购物详情的页面
- angular+做一个购物车可以删除,计算总额
- 淘宝的购物
- 利用javascript做的一个用户可自定义输入框的登陆页面
- 用c#做的一个目录树(可新建、删除、重命名)
- iOS开发之如何做一个可上下左右滑动的页面
- 设计一个实用、可自动化测试的智能购物车
- 使用AngularJS和HTML做淘宝中购物车的一些功能
- JQuery和JS怎样实现淘宝购物车的添加和删除?
- 做一个可编辑的表格控件
- 做一个可编辑的表格控件
- 做一个可编辑的表格控件
- 做一个可编辑的表格控件
- 做一个可编辑的表格控件
- Entoloma saundersii Mushroom
- 一元二次方程
- iOS-TextView、UIButton加边框,加下划线
- 全球最大的搜索引擎排名~~~~~~~~!!!!
- 每个Mac OS X 命令行用户应当知道的八个终端工具
- 做一个可删除的淘宝购物页面
- 用NPOI把数据库内容导入到Excel中实例(NPOI版本2.0)
- MyBatis传入多个参数的问题
- ======Gson======
- (1) Mysql ---- ABC 数据库基本操作、数据类型和运算符、函数 《MYSQL 5.5从零开始学》笔记
- 服装020 远远不只是一个APP
- AS3自带的三种碰撞检测方式
- 使用J-LINK烧写MICRO2440的NOR
- 一元二次方程