TableView 实现京东购物车功能

来源:互联网 发布:记账软件 编辑:程序博客网 时间:2024/05/01 13:04
1、首先创建tableview,设置多选状态:self.tableView.allowsMultipleSelectionDuringEditing = YES;
2、设置编辑按钮
 
//编辑按钮

self.enditButton = [[UIBarButtonItem allocinitWithTitle:@"删除" style:UIBarButtonItemStylePlain target:selfaction:@selector(editButtonDidClick:)];

self.navigationItem.rightBarButtonItem = self.enditButton;

#pragma mark - 删除按钮点击事件

- (void)editButtonDidClick:(UIBarButtonItem *)editButton

{

    //设置编辑状态

    if (self.tableView.editing == YES) {

        [editButton setTitle:@"删除"];

        [self.tableView setEditing:NO animated:YES];

    }else

    {

        [editButton setTitle:@"取消"];

        [self.tableView setEditing:YES animated:YES];

    }

    

    if (self.tableView.editing) {

        if (self.deleteButtonBack == nil) {

            self.deleteButton = [[UIButton allocinitWithFrame:CGRectMake(1815YJScreenWidth - 3040)];

            [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];

            [self.deleteButton setTitleColor:[UIColor whiteColorforState:UIControlStateNormal];

            [self.deleteButton setBackgroundColor:YJColorRGBA(24248481)];

            self.deleteButton.layer.cornerRadius = 3;

            self.deleteButton.layer.masksToBounds = YES;

            [self.deleteButton addTarget:self action:@selector(deleteButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];

            

            if ([self.isMoreInput isEqualToString:@"YES"]) {

                self.deleteButtonBack = [[UIView allocinitWithFrame:CGRectMake(0YJScreenHeight - 57YJScreenWidth75)];

            }else{

                self.deleteButtonBack = [[UIView allocinitWithFrame:CGRectMake(0YJScreenHeight - 57 - 44YJScreenWidth75)];

            }

            self.deleteButtonBack.backgroundColor = [UIColor whiteColor];

            

            [self.deleteButtonBack addSubview:self.deleteButton];

            

            [self.view addSubview:self.deleteButtonBack];

            [self.view bringSubviewToFront:self.deleteButton];

            

            [UIView animateWithDuration:0.25 animations:^{

                self.deleteButtonBack.transform = CGAffineTransformMakeTranslation(0, -75);

                editButton.enabled = NO;

            } completion:^(BOOL finished) {

                editButton.enabled = YES;

                if ([self.isMoreInput isEqualToString:@"YES"]) {

                    self.tableVonstraint.constant = 70;

                }else{

                    self.tableVonstraint.constant = 110;

                }

            }];

        }

    }else

    {

        self.tableVonstraint.constant = 0;

        if (self.deleteButtonBack != nil) {

            [UIView animateWithDuration:0.25 animations:^{

                self.deleteButtonBack.transform = CGAffineTransformIdentity;

                editButton.enabled = NO;

            } completion:^(BOOL finished) {

                [self.deleteButtonBack removeFromSuperview];

                self.deleteButtonBack = nil;

                editButton.enabled = YES;

            }];

        }

    }

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self.tableView reloadData];

    });


}

/**

 *  删除按钮点击事件

 *

 *  @param deleteButton 删除按钮

 */

- (void)deleteButtonDidClick:(UIButton *)deleteButton

{

    

} 

3、遵循tableview的代理,设置选中和取消状态

#pragma mark - TableViewDelegate

//选中

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

{

    MoreCollectionModel * model = self.dataSourceArray[indexPath.section];

    if (indexPath.row == 0) {

        for (int i = 0; i < model.goodsArray.count; i++) {

            NSIndexPath * index = [NSIndexPath indexPathForRow:i + 1 inSection:indexPath.section];

            [tableView selectRowAtIndexPath:index animated:YES scrollPosition:UITableViewScrollPositionNone];

        }

    }else

    {

        NSArray * indexArray = [tableView indexPathsForSelectedRows];

        NSInteger indexCount = 0;

        for (NSIndexPath * index in indexArray) {

            if (index.section == indexPath.section) {

                indexCount ++;

            }

        }

        if (indexCount == (model.goodsArray.count)) {

            [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.sectionanimated:YESscrollPosition:UITableViewScrollPositionNone];

        }

    }

    NSArray * array = [self.tableView indexPathsForSelectedRows];

    [self.indexPaths addObjectsFromArray:array];

}

//取消选中

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

{

    MoreCollectionModel * model = self.dataSourceArray[indexPath.section];

    if (indexPath.row == 0) {

        for (int i = 0; i < model.goodsArray.count; i++) {

            NSIndexPath * index = [NSIndexPath indexPathForRow:i + 1 inSection:indexPath.section];

            [tableView deselectRowAtIndexPath:index animated:YES];

        }

    }else

    {

        NSArray * indexArray = [tableView indexPathsForSelectedRows];

        NSInteger indexCount = 0;

        for (NSIndexPath * index in indexArray) {

            if (index.section == indexPath.section) {

                indexCount ++;

            }

        }

        if ((indexCount - 1)  != (model.goodsArray.count)) {

            [tableView deselectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.sectionanimated:YES];

        }

    }

    NSArray * array = [self.tableView indexPathsForSelectedRows];

    [self.indexPaths removeObjectsInArray:array];

}

 4、效果如下图:

1 0
原创粉丝点击