购物车界面,不同section,点击增减物品,确定取消选中的逻辑判断

1、首先在自定义的cell中,创建两个代理方法
<pre>
@protocol shopCartDelegate <NSObject>

-(void)shopCartDelegate:(ShopCartCell *)cell WithBtn:(UIButton *)btn;
-(void)shopCartDelegateNum:(ShopCartCell *)cell WithBtn:(UIButton *)btn;

@end

@interface ShopCartCell : UITableViewCell
@property(nonatomic,assign)id<shopCartDelegate>delegate;
@end
</pre>

2、在viewcontroller中实现这两个代理方法
shopCell.delegate = self;//遵循代理
方法实现

(1)选中和取消的代理方法
<pre>
-(void)shopCartDelegate:(ShopCartCell *)cell WithBtn:(UIButton *)btn

{
NSIndexPath *indexPath =[self.tableView indexPathForCell:cell];
int couss=0 ;
NSArray *array =self.array[indexPath.section];
for (int i = 0; i < array.count; i++) {
ShopModel *model =array[i];
if (i==indexPath.row) {
if (btn.selected) {
model.isSelected = @"true";
}else{
model.isSelected = @"false";
}
}
if ([model.isSelected isEqualToString:@"true"]) {
couss++;
}
}

//判断section内的cell是否都被选中
if (couss == array.count) {
self.isonOff = NO;
}else{
self.isonOff = YES;
}
[self.tableView reloadData];
}
</pre>
(2)增加减少
<pre>
-(void)shopCartDelegateNum:(ShopCartCell *)cell WithBtn:(UIButton *)btn

{

NSIndexPath *indexPath =[self.tableView indexPathForCell:cell];

NSArray *array =self.array[indexPath.section];

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

    ShopModel *model =array[i];

    if (i==indexPath.row) {

        if (btn.tag == 2000) {

            model.num =[NSString stringWithFormat:@"%d",model.num.intValue+1];

        }else{

            if (model.num.intValue == 1) { 

            }else{

            model.num =[NSString stringWithFormat:@"%d",model.num.intValue-1];

            }

        }

    }

}

[self.tableView reloadData];

}
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容