tabletableView的单选(系统对勾) 自定义的单选(自定义图片)

//选择转账用户对勾

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

{

    NSInteger newRow = [indexPathrow];

    NSInteger oldRow = (_lastPath ==nil)?[_lastPathrow]:-1;

    if (newRow != oldRow) {

        UITableViewCell *newCell = [tableViewcellForRowAtIndexPath:indexPath];

        newCell.accessoryType =UITableViewCellAccessoryCheckmark;

        UITableViewCell *oldCell = [tableViewcellForRowAtIndexPath:_lastPath];

        oldCell.accessoryType =UITableViewCellAccessoryNone;

        _lastPath = indexPath;

    }

    [tableView deselectRowAtIndexPath:indexPathanimated:YES];

    _selectSourceM  = _dataSourceM[newRow];

}



下面是自定义的单选

创建Model 设置个BOOL值

#import <Foundation/Foundation.h>


@interface Model : NSObject


@property(nonatomic, assign) BOOL isSelected;


@end



cell.h

#import <UIKit/UIKit.h>

@class Model;

@interface LYTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIButton *btn;

@property(assign, nonatomic)NSInteger clickCount;

- (void)cellWithData:(Model *)model;


自定义cell .m

- (void)cellWithData:(Model *)model {

    if (model.isSelected) {

        [self.btn setBackgroundImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];

    } else {

        [self.btn setBackgroundImage:[UIImage imageNamed:@"未选中"] forState:UIControlStateNormal];

    }

}



tableView 中尽享单选处理

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    

    [cell.btn addTarget:self action:@selector(ClickBtn:) forControlEvents:UIControlEventTouchUpInside];

    cell.btn.tag = indexPath.row;

    Model *model =self.arrays[indexPath.row];

  

    [cell cellWithData:model];

    return cell;

}


-(void)ClickBtn:(UIButton *)btn

{

   if (self.selectModel) {

        self.selectModel.isSelected = !self.selectModel.isSelected;

    }

    Model *model = self.arrays[btn.tag];

    if (!model.isSelected) {

        model.isSelected = !model.isSelected;

        self.selectModel = model;

    }

    [self.tableView reloadData];

}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值