你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios UITableView單元格多選框的完成

ios UITableView單元格多選框的完成

編輯:IOS開發綜合

該文章針對的是購物車多選和多選金額算計需求的完成,在開發中極易遇到的幾個問題是:1、復選問題,2、單元格重用問題,3、全選問題,4、統計問題。

其中在復選問題之外還有一個是單個商品購置數量增減問題。

以下是代碼局部:(注:改代碼為獨立局部,並沒有停止數據懇求,運用的假數據,可直接拷貝或許下載,在團體工程中運轉運轉測試)。

其中:JC_BaseViewController.h是團體自定義的base控制器,子類中調用局部可相應刪除

ShoppingViewController.h

#import "JC_BaseViewController.h"

@interface ShoppingViewController : JC_BaseViewController
@property (nonatomic, strong) UITableView *tableView;

@end


ShoppingViewController.m

//購物車
/**
 *_selectBtn.tag = 100;
 *numLabel.tag = 200;
 *addBTN.tag = 300;
 *redueBTN.tag = 400;
 *
 */

#import "ShoppingViewController.h"
#import "ShoppingTableViewCell.h"
@interface ShoppingViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UIView              *headerView;
    NSMutableArray      *dataArray;
    
    //底部視圖
    UIView              *bottomBgView;
    UIButton            *quanxBtn;              //全選
    UILabel             *hejiLabel;             //算計
    UILabel             *shoufuLabel;           //首付、月供
    UIButton            *submitBTN;             //結算
    
    //添加按鈕
    UIButton            *reduceBTN;             //減
    UIButton            *addBTN;                //加
    UILabel             *numLabel;              //數量
    NSMutableArray      *selectArray;           //選擇形態數組
    NSMutableArray      *numArray;              //購置數量
    
    //算計
    float               hejiMoney;              //算計金額
    int                 selectNum;              //選中數量
    
}
@property (nonatomic, strong) UIButton          *selectBtn;             //復選框
@end
//#define JC_ARCM_COLOR [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 0.1 alpha:1.0]
#define JC_ARCM_COLOR [UIColor clearColor]

@implementation ShoppingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"購物車";
    dataArray = [[NSMutableArray alloc]init];
    [self requestData];
    [self.view addSubview:self.tableView];
    [self initFooterViews];
    [self initBottomViews];
    [self.tableView addHeaderWithtarget:self
                                 action:@selector(requestData)];
    [self.tableView addFooterWithtarget:self
                                 action:@selector(requestData)];
    
    
}
//底部視圖
- (void)initBottomViews
{
    bottomBgView = [[UIView alloc]init];
    bottomBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBgView];
    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(bottomBgView.superview);
        make.height.mas_equalTo(60);
    }];
    submitBTN = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBTN.backgroundColor = COLOR_TITLE_RED__;
    [submitBTN setTitle:@"結算(0)" forState:UIControlStateNormal];
    submitBTN.titleLabel.font = [UIFont boldSystemFontOfSize:15.0];
    [submitBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomBgView addSubview:submitBTN];
    [submitBTN mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(submitBTN.superview);
        make.width.mas_equalTo(85);
    }];
    
    quanxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    quanxBtn.selected = 0;
    [quanxBtn setImage:[UIImage imageNamed:@"select_nor"] forState:UIControlStateNormal];
    [quanxBtn addTarget:self
                 action:@selector(quanxuanAction:)
       forControlEvents:UIControlEventTouchUpInside];
    [bottomBgView addSubview:quanxBtn];
    [quanxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.centerY.equalTo(quanxBtn.superview);
        make.width.height.mas_equalTo(40);
    }];
    hejiLabel = [[UILabel alloc]init];
    hejiLabel.font = [UIFont boldSystemFontOfSize:16.0];
    hejiLabel.textColor = COLOR_TITLE_RED__;
    hejiLabel.text = @"算計:0元";
    [bottomBgView addSubview:hejiLabel];
    [hejiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(quanxBtn.mas_right);
        make.top.mas_equalTo(10);
    }];
    shoufuLabel = [[UILabel alloc]init];
    shoufuLabel.textColor = COLOR_TITLE_GRAY__;
    shoufuLabel.font = [UIFont systemFontOfSize:14.0];
    shoufuLabel.text = @"首付:0元 月供:0元";
    [bottomBgView addSubview:shoufuLabel];
    [shoufuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hejiLabel);
        make.top.equalTo(hejiLabel.mas_bottom).with.offset(5);
        make.right.equalTo(shoufuLabel.superview).with.offset(-85);
    }];
    
}

//尾視圖
- (void)initFooterViews
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 30)];
    footerView.backgroundColor = COLOR_VC_BG__;
    _tableView.tableFooterView = footerView;
}
#pragma mark -
#pragma mark - 表視圖
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64 - 60) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
#pragma mark -
#pragma mark - UITableViewDelegate
//定義展現的Cell的個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

//定義展現的Section的個數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idetifier = @"cell";
//    NSString *idetifier = [NSString stringWithFormat:@"cell%zi", indexPath.row];
    ShoppingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:idetifier];
    if (cell == nil)
    {
        cell = [[ShoppingTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idetifier];
        
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.selected = 0;
        _selectBtn.tag = 100;
        _selectBtn.userInteractionEnabled = NO;
//        _selectBtn.backgroundColor = [UIColor redColor];
        [_selectBtn setImage:[UIImage imageNamed:@"select_nor"] forState:UIControlStateNormal];
        [cell.contentView addSubview:_selectBtn];
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10);
            make.centerY.equalTo(_selectBtn.superview);
            make.width.height.mas_equalTo(30);
        }];
        
        addBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBTN setImage:[UIImage imageNamed:@"addIcon"] forState:UIControlStateNormal];
        addBTN.tag = 300;
        [addBTN addTarget:self
                   action:@selector(addAction:)
         forControlEvents:UIControlEventTouchUpInside];
        [cell.bgView addSubview:addBTN];
        addBTN.backgroundColor = JC_ARCM_COLOR;
        [addBTN mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(addBTN.superview).with.offset(2.5);
            make.centerY.equalTo(cell.priceLabel);
            make.width.height.mas_equalTo(40);
        }];
        numLabel = [[UILabel alloc]init];
        numLabel.text = @"1";
        numLabel.tag = 200;
        numLabel.textColor = COLOR_TITLE_BLACK__;
        numLabel.textAlignment = NSTextAlignmentCenter;
        numLabel.font = [UIFont systemFontOfSize:14.0];
//        numLabel.backgroundColor = JC_ARCM_COLOR;
        [cell.bgView addSubview:numLabel];
        [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.priceLabel);
            make.right.equalTo(addBTN.mas_left).with.offset(7.5);
            make.width.mas_equalTo(30);
            make.height.mas_equalTo(20);
        }];
        reduceBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        reduceBTN.tag = 400;
        reduceBTN.backgroundColor = JC_ARCM_COLOR;
        [reduceBTN setImage:[UIImage imageNamed:@"reduceIcon"] forState:UIControlStateNormal];
        [reduceBTN addTarget:self
                      action:@selector(reduceAction:)
            forControlEvents:UIControlEventTouchUpInside];
        [cell.bgView addSubview:reduceBTN];
        [reduceBTN mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(numLabel.mas_left).with.offset(7.5);
            make.centerY.equalTo(cell.priceLabel);
            make.width.height.mas_equalTo(40);
        }];

    }

        cell.backgroundColor = JC_ARCM_COLOR;
//    cell.backgroundColor = COLOR_VC_BG__;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (dataArray != nil)
    {
        cell.nameLabel.text = [dataArray[indexPath.row] objectForKey:@"name"];
        cell.goodImgView.image = [UIImage imageNamed:[dataArray[indexPath.row] objectForKey:@"img_path"]];
        cell.describeLabel.text = [dataArray[indexPath.row] objectForKey:@"describe"];
        cell.sxLabel.text = [dataArray[indexPath.row] objectForKey:@"shuxing"];
        NSDictionary *attributeDict_price = [NSDictionary dictionaryWithObjectsAndKeys:
                                             [UIFont boldSystemFontOfSize:20.0],NSFontAttributeName,
                                             COLOR_TITLE_RED__,NSForegroundColorAttributeName,nil];
        NSMutableAttributedString *attributedStr_price = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%@", [dataArray[indexPath.row] objectForKey:@"price"]] attributes:attributeDict_price];
        cell.priceLabel.attributedText = attributedStr_price;
        cell.shoufuLabel.text = [dataArray[indexPath.row] objectForKey:@"shoufu"];
        cell.yueGongLabel.text = [dataArray[indexPath.row] objectForKey:@"yuegong"];

        //選擇按鈕
        UIButton *selectbutton = [cell.contentView viewWithtag:100];
        selectbutton.selected = [[selectArray objectAtIndex:indexPath.row] boolValue];
        if (selectbutton.selected == 1)
        {
            [selectbutton setImage:[UIImage imageNamed:@"select_sel"] forState:UIControlStateNormal];
        }
        else
        {
            [selectbutton setImage:[UIImage imageNamed:@"select_nor"] forState:UIControlStateNormal];
        }
        //購置件數
        UILabel *numlabel = [cell.contentView viewWithTag:200];
        NSLog(@"%@", numArray);
        numlabel.text = [numArray objectAtIndex:indexPath.row];
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([selectArray[indexPath.row] isEqualToString:@"0"]) {
        [selectArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
    }
    else
    {
        [selectArray replaceObjectAtIndex:indexPath.row withObject:@"0"];
    }
    //刷新選中的一行
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    selectNum = 0;
    for (int i = 0; i < selectArray.count; i ++)
    {
        NSString *issel = selectArray[i];//每個cell的選擇形態
        if ([issel isEqualToString:@"1"])
        {//記載選中個數
            selectNum = selectNum + 1;
        }
        else
        {//假如存在有未選中cell,全選按鈕形態為0
            [quanxBtn setImage:[UIImage imageNamed:@"select_nor"] forState:UIControlStateNormal];
            quanxBtn.selected = 0;
        }
        if (selectNum == selectArray.count)
        {//假如經過點擊cell全部選中了,改動全選按鈕形態--選中個數等於全部個數
            [quanxBtn setImage:[UIImage imageNamed:@"select_sel"] forState:UIControlStateNormal];
            quanxBtn.selected = 1;
        }
    }
    //計算金額
    [self selectNumCalculate];
    [submitBTN setTitle:[NSString stringWithFormat:@"結算(%d)", selectNum] forState:UIControlStateNormal];

}

#pragma mark -
#pragma mark - 按鈕點擊事情
//全選按鈕
- (void)quanxuanAction:(UIButton *)button
{
    button.selected = !button.selected;
    if (button.selected == 1)
    {
        [button setImage:[UIImage imageNamed:@"select_sel"] forState:UIControlStateNormal];
    }
    else
    {
        [button setImage:[UIImage imageNamed:@"select_nor"] forState:UIControlStateNormal];
    }
    //一切cell的選中形態
    for (int i = 0; i < dataArray.count; i ++)
    {
        if (button.selected == 0)
        {
            [selectArray replaceObjectAtIndex:i withObject:@"0"];
        }
        else
        {
            [selectArray replaceObjectAtIndex:i withObject:@"1"];
        }
    }
    [_tableView reloadData];
    
    
    NSLog(@"money = %.0f", hejiMoney);
    //調用算計金額計算辦法
    [self selectNumCalculate];
    [submitBTN setTitle:[NSString stringWithFormat:@"結算(%d)", selectNum] forState:UIControlStateNormal];

}
//減
- (void)reduceAction:(UIButton *)button
{
    NSIndexPath *indexpath =  [_tableView indexPathForCell:(ShoppingTableViewCell *) button.superview.superview.superview];
    if ([numArray[indexpath.row] intValue] == 1)
    {
        [JCTipView showWithString:@"單次購置最少1件"];
    }
    else
    {
        [numArray replaceObjectAtIndex:indexpath.row withObject:[NSString stringWithFormat:@"%d", [numArray[indexpath.row] intValue] - 1]];
        //一個cell刷新
        [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexpath,nil]
                          withRowAnimation:UITableViewRowAnimationNone];
    }
    NSLog(@"--%@", numArray);
    //依據選中的單元格的實踐購置數量計算金額
    [self hejiCalculate];
}
//加
- (void)addAction:(UIButton *)button
{
    NSIndexPath *indexpath =  [_tableView indexPathForCell:(ShoppingTableViewCell *) button.superview.superview.superview];
    if ([numArray[indexpath.row] intValue] == 99)
    {
        [JCTipView showWithString:@"單次購置最多99件"];
    }
    else
    {
        [numArray replaceObjectAtIndex:indexpath.row withObject:[NSString stringWithFormat:@"%d", [numArray[indexpath.row] intValue] + 1]];
        
        //一個cell刷新
        [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexpath,nil]
                          withRowAnimation:UITableViewRowAnimationNone];
    }
    
    //依據選中的單元格的實踐購置數量計算金額
    [self hejiCalculate];
    
}

#pragma mark - 
#pragma mark - 金額計算
//購置數量計算
- (void)selectNumCalculate
{
    //計算
    selectNum = 0;
    for (int i = 0; i < selectArray.count; i ++)
    {
        NSString *issel = selectArray[i];
        if ([issel isEqualToString:@"1"])
        {
            selectNum = selectNum + 1;
        }
    }
    [self hejiCalculate];
}

//算計
- (void)hejiCalculate
{
    //依據選中的單元格的實踐購置數量計算金額
    hejiMoney = 0;
    for (int i = 0; i < selectArray.count; i ++)
    {
        if ([selectArray[i] isEqualToString:@"1"])
        {
            hejiMoney = hejiMoney + [[[dataArray objectAtIndex:i] objectForKey:@"price"] intValue] * [[numArray objectAtIndex:i] intValue];
        }
    }
    hejiLabel.text = [NSString stringWithFormat:@"算計:%.2f元", hejiMoney];
}

#pragma mark -
#pragma mark - 數據懇求
- (void)requestData
{
    [self.tableView headerEndRefreshing];
    [self.tableView footerEndRefreshing];
    dataArray = [[NSMutableArray alloc]initWithObjects:@{@"name" : @"纨绔子弟旗艦店",
                                                         @"img_path" : @"good_1",
                                                         @"describe" : @"纨绔子弟男鞋夏季新款  男士休閒皮鞋男韓版棉鞋青年潮鞋",
                                                         @"shuxing" : @"顏色:藏青色; 尺碼:41",
                                                         @"price" : @"1000",
                                                         @"shoufu" : @"首付:100.98元",
                                                         @"yuegong" : @"月供:302.50元x24期"},
                                                         @{@"name" : @"361度旗艦店",
                                                           @"img_path" : @"good_2",
                                                           @"describe" : @"361度男鞋清倉  夏季情侶男士休閒鞋361運動鞋青少年白色滑板鞋鞋子",
                                                           @"shuxing" : @"顏色:白色; 尺碼:41",
                                                           @"price" : @"2000",
                                                           @"shoufu" : @"首付:10.02元",
                                                           @"yuegong" : @"月供:10.02元x15期"},
                                                         @{@"name" : @"普華錦訊數碼專營店",
                                                           @"img_path" : @"mobileIcon",
                                                           @"describe" : @"Lenvov/聯想 A3910e70挪動智能聯通電信全網通版4G雙卡智能手機正品",
                                                           @"shuxing" : @"顏色:清爽白; 套餐:官方標配; 存儲容量:8G; 版本類型:中國大陸",
                                                           @"price" : @"3000",
                                                           @"shoufu" : @"首付:256.34元",
                                                           @"yuegong" : @"月供:256.34元x9期"},
                                                         @{@"name" : @"roaringwild旗艦店",
                                                           @"img_path" : @"fzIcon",
                                                           @"describe" : @"ROARINGWILD 16AW SOUTIENCOLLAR/C-JKT 黑色華達呢長風衣",
                                                           @"shuxing" : @"顏色:清爽白; 套餐:官方標配; 存儲容量:8G; 版本類型:中國大陸",
                                                           @"price" : @"4000",
                                                           @"shoufu" : @"首付:50元",
                                                           @"yuegong" : @"月供:50元x18期"},
                                                         @{@"name" : @"纨绔子弟旗艦店",
                                                           @"img_path" : @"good_1",
                                                           @"describe" : @"纨绔子弟男鞋夏季新款  男士休閒皮鞋男韓版棉鞋青年潮鞋",
                                                           @"shuxing" : @"顏色:藏青色; 尺碼:41",
                                                           @"price" : @"5000",
                                                           @"shoufu" : @"首付:100.98元",
                                                           @"yuegong" : @"月供:302.50元x24期"},
                                                         @{@"name" : @"361度旗艦店",
                                                           @"img_path" : @"good_2",
                                                           @"describe" : @"361度男鞋清倉  夏季情侶男士休閒鞋361運動鞋青少年白色滑板鞋鞋子",
                                                           @"shuxing" : @"顏色:白色; 尺碼:41",
                                                           @"price" : @"6000",
                                                           @"shoufu" : @"首付:10.02元",
                                                           @"yuegong" : @"月供:10.02元x15期"},
                                                         @{@"name" : @"普華錦訊數碼專營店",
                                                           @"img_path" : @"mobileIcon",
                                                           @"describe" : @"Lenvov/聯想 A3910e70挪動智能聯通電信全網通版4G雙卡智能手機正品",
                                                           @"shuxing" : @"顏色:清爽白; 套餐:官方標配; 存儲容量:8G; 版本類型:中國大陸",
                                                           @"price" : @"7000",
                                                           @"shoufu" : @"首付:256.34元",
                                                           @"yuegong" : @"月供:256.34元x9期"},
                                                         @{@"name" : @"roaringwild旗艦店",
                                                           @"img_path" : @"fzIcon",
                                                           @"describe" : @"ROARINGWILD 16AW SOUTIENCOLLAR/C-JKT 黑色華達呢長風衣",
                                                           @"shuxing" : @"顏色:清爽白; 套餐:官方標配; 存儲容量:8G; 版本類型:中國大陸",
                                                           @"price" : @"8000",
                                                           @"shoufu" : @"首付:50元",
                                                           @"yuegong" : @"月供:50元x18期"},
                                                         @{@"name" : @"纨绔子弟旗艦店",
                                                           @"img_path" : @"good_1",
                                                           @"describe" : @"纨绔子弟男鞋夏季新款  男士休閒皮鞋男韓版棉鞋青年潮鞋",
                                                           @"shuxing" : @"顏色:藏青色; 尺碼:41",
                                                           @"price" : @"9000",
                                                           @"shoufu" : @"首付:100.98元",
                                                           @"yuegong" : @"月供:302.50元x24期"},
                                                         @{@"name" : @"361度旗艦店",
                                                           @"img_path" : @"good_2",
                                                           @"describe" : @"361度男鞋清倉  夏季情侶男士休閒鞋361運動鞋青少年白色滑板鞋鞋子",
                                                           @"shuxing" : @"顏色:白色; 尺碼:41",
                                                           @"price" : @"10000",
                                                           @"shoufu" : @"首付:10.02元",
                                                           @"yuegong" : @"月供:10.02元x15期"},
                                                         @{@"name" : @"普華錦訊數碼專營店",
                                                           @"img_path" : @"mobileIcon",
                                                           @"describe" : @"Lenvov/聯想 A3910e70挪動智能聯通電信全網通版4G雙卡智能手機正品",
                                                           @"shuxing" : @"顏色:清爽白; 套餐:官方標配; 存儲容量:8G; 版本類型:中國大陸",
                                                           @"price" : @"12000",
                                                           @"shoufu" : @"首付:256.34元",
                                                           @"yuegong" : @"月供:256.34元x9期"},
                                                         @{@"name" : @"roaringwild旗艦店",
                                                           @"img_path" : @"fzIcon",
                                                           @"describe" : @"ROARINGWILD 16AW SOUTIENCOLLAR/C-JKT 黑色華達呢長風衣",
                                                           @"shuxing" : @"顏色:清爽白; 套餐:官方標配; 存儲容量:8G; 版本類型:中國大陸",
                                                           @"price" : @"13000",
                                                           @"shoufu" : @"首付:50元",
                                                           @"yuegong" : @"月供:50元x18期"},
                                                         nil];
    
    if (selectArray == nil || selectArray.count < dataArray.count)
    {
        selectArray = [[NSMutableArray alloc]init];
        numArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < dataArray.count; i ++)
        {
            [selectArray addObject:@"0"];
            [numArray addObject:@"1"];
        }
    }
   
    [_tableView reloadData];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


ShoppingTableViewCell.h


#import <UIKit/UIKit.h>

@interface ShoppingTableViewCell : UITableViewCell
{
    UIView *lineView_1;
    UIView *lineView_2;
}
@property (nonatomic, strong) UIView            *bgView;
@property (nonatomic, strong) UILabel           *nameLabel;             //稱號
@property (nonatomic, strong) UIImageView       *goodImgView;
@property (nonatomic, strong) UILabel           *describeLabel;         //描繪
@property (nonatomic, strong) UILabel           *priceLabel;            //價錢
@property (nonatomic, strong) UILabel           *sxLabel;               //商品屬性
@property (nonatomic, strong) UILabel           *shoufuLabel;           //首付
@property (nonatomic, strong) UILabel           *yueGongLabel;          //月供
@end

ShoppingTableViewCell.m


#import "ShoppingTableViewCell.h"
//#define JC_ARCM_COLOR [UIColor colorWithRed:arc4random() % 10 * 0.1 green:arc4random() % 10 * 0.1 blue:arc4random() % 10 * 0.1 alpha:1.0]
#define JC_ARCM_COLOR [UIColor clearColor]
@implementation ShoppingTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //...
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_bgView];
        _bgView.frame = CGRectMake(7.5, 10, KScreenWidth - 15, 190);
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.masksToBounds = YES;
        [self initTopViews];
        [self initCenterViews];
        [self initBottomViews];
    }
    return self;
}

//頂部
- (void)initTopViews
{
    _nameLabel = [[UILabel alloc]init];
    _nameLabel.text = @"首爾韓伊";
    _nameLabel.backgroundColor = JC_ARCM_COLOR;
    _nameLabel.font = [UIFont systemFontOfSize:15.0];
    _nameLabel.textColor = COLOR_TITLE_BLACK__;
    [_bgView addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.equalTo(_nameLabel.mas_right).with.offset(-10);
        make.top.mas_equalTo(10);
    }];
    lineView_1 = [[UIView alloc]init];
    lineView_1.backgroundColor = COLOR_VC_BG__;
    [_bgView addSubview:lineView_1];
    [lineView_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(lineView_1.superview);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(_nameLabel.mas_bottom).with.offset(7.5);
    }];
}
//中部
- (void)initCenterViews
{
    _goodImgView = [[UIImageView alloc]init];
    _goodImgView.contentMode = UIViewContentModeScaleaspectFit;
    [_bgView addSubview:_goodImgView];
    [_goodImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel);
        make.top.equalTo(lineView_1.mas_bottom).with.offset(10);
        make.width.height.mas_equalTo(80);
    }];
    _goodImgView.image = [UIImage imageNamed:@"good_2"];
    _describeLabel = [[UILabel alloc]init];
    _describeLabel.font = [UIFont systemFontOfSize:15.0];
    _describeLabel.textColor = COLOR_TITLE_BLACK__;
    _describeLabel.numberOfLines = 2;
    [_bgView addSubview:_describeLabel];
    _describeLabel.text = @"COCACH蔻馳 手提包撒奧所多德豐多歲的得打多水水水水歲馳 手提包撒奧所";
    [_describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_goodImgView.mas_right).with.offset(10);
        make.top.equalTo(lineView_1.mas_bottom).with.offset(10);
        make.right.equalTo(_describeLabel.superview.mas_right).with.offset(-10);
    }];
    _sxLabel = [[UILabel alloc]init];
    _sxLabel.textColor = COLOR_TITLE_GRAY__;
    _sxLabel.font = [UIFont systemFontOfSize:14.0];
    [_bgView addSubview:_sxLabel];
    [_sxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_describeLabel);
        make.top.equalTo(_describeLabel.mas_bottom).with.offset(5);
    }];
    _sxLabel.text = @"顏色:黑色; 尺碼:S";
    _priceLabel = [[UILabel alloc]init];
    [_bgView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_describeLabel);
        make.top.equalTo(_sxLabel.mas_bottom).with.offset(5);
    }];
    
    
    lineView_2 = [[UIView alloc]init];
    lineView_2.backgroundColor = COLOR_VC_BG__;
    [_bgView addSubview:lineView_2];
    [lineView_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(lineView_2.superview);
        make.top.equalTo(_priceLabel.mas_bottom).with.offset(10);
        make.height.mas_equalTo(1);
    }];
    
}
//底部
- (void)initBottomViews
{
    _shoufuLabel = [[UILabel alloc]init];
    _shoufuLabel.text = @"首付:10000元";
    _shoufuLabel.textColor = COLOR_TITLE_GRAY__;
    _shoufuLabel.font = [UIFont systemFontOfSize:14.0];
    [_bgView addSubview:_shoufuLabel];
    [_shoufuLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.equalTo(lineView_2.mas_bottom);
        make.height.mas_equalTo(45);
    }];
    _yueGongLabel = [[UILabel alloc]init];
    _yueGongLabel.textColor = COLOR_TITLE_GRAY__;
    _yueGongLabel.font = [UIFont systemFontOfSize:14.0];
    _yueGongLabel.text = @"月供:10000.99x24期";
    [_bgView addSubview:_yueGongLabel];
    [_yueGongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_shoufuLabel.mas_right).with.offset(15);
        make.right.equalTo(_yueGongLabel.superview).with.offset(-10);
        make.top.bottom.equalTo(_shoufuLabel);
    }];
}

@end


IOS UITableView單元格多選框的實現【ios UITableView單元格多選框的完成】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved