应用程序之淘宝商城界面模拟

  • 效果展示
  • 代码实现

一、效果展示

二、代码实现

1⃣️数据源

2⃣️代码实现

//
//  ViewController.m
//  03-淘宝商品001
//
//  Created by apple on 14-4-3.
//  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//

#import "ViewController.h"
#import "Shops.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *shopArray;
    NSMutableArray *selectShop;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"shops" ofType:@"plist"];
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
    shopArray = [NSMutableArray array];
    selectShop = [NSMutableArray array];
    
    for (NSDictionary *dict in tempArray) {
        Shops *shop = [Shops showWithDict:dict];
        [shopArray addObject:shop];
    }
}
//得到cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (selectShop.count == 0) {
        _titleLable.text = @"淘宝";
        _removeItem.enabled = NO;
    } else {
        _titleLable.text = [NSString stringWithFormat:@"淘宝(%d)", selectShop.count];
        _removeItem.enabled = YES;
    }
    return shopArray.count;
}
//刷新cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Shops *shop = shopArray[indexPath.row];
    
    static NSString *cellIndentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];
    }
    cell.textLabel.text = shop.title;
    cell.detailTextLabel.text = shop.desc;
    cell.imageView.image = [UIImage imageNamed:shop.icon];
    
    if ([selectShop containsObject:shop]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中时的蓝色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    Shops *shop = shopArray[indexPath.row];
    
    if ([selectShop containsObject:shop]) {
        [selectShop removeObject:shop];
    } else {
        [selectShop addObject:shop];
    }
    
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(void)deleteCell
{
    [shopArray removeObjectsInArray:selectShop];
    
    [selectShop removeAllObjects];
    
    [self.tableView reloadData];
}
@end
原文地址:https://www.cnblogs.com/letougaozao/p/3652720.html