UITbaleView上按鈕的單選,uitbaleview單選

來源:互聯網
上載者:User

UITbaleView上按鈕的單選,uitbaleview單選

設定Id屬性,標記是哪個cell

@property (nonatomic,assign)NSInteger Id;

設定一個普通狀態和選中狀態圖片不同的按鈕

  _choose = [[UIButton alloc]init];        [_choose setImage:[UIImage imageNamed:@"about_未勾選"] forState:UIControlStateNormal];        [_choose setImage:[UIImage imageNamed:@"about_勾選"] forState:UIControlStateSelected];        [_choose addTarget:self action:@selector(choosePressed:) forControlEvents:UIControlEventTouchUpInside];        [self.contentView addSubview:_choose];
- (void)layoutSubviews {    _choose.frame = CGRectMake(10, 120+wei_tiao, 20, 20);}

設定一個可調整選中或者為選中的方法

- (void)setChecked:(BOOL)checked {    if (checked) {        _choose.selected = YES;    }else {        _choose.selected = NO;    }}

cell上按鈕的單擊事件

/** *  選擇勾選 * *  @param sender 按鈕 */- (void)choosePressed:(UIButton *)sender {    sender.selected = YES;    if (_chooseBlock) {        _chooseBlock(self.Id);    }}

設定一個全域變數來判定點了哪一個cell

    NSInteger _cellIndex;

 

tableView代理:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];    cell.selectionStyle = UITableViewCellSelectionStyleNone;    cell.Id = indexPath.row;    if (_cellIndex == indexPath.row) {        [cell setChecked:YES];    }else {        [cell setChecked:NO];    }    [cell setChooseBlock:^(NSInteger index) {        _cellIndex = index;        [_tableView reloadData];    }];    return cell;}

 

補:使用了block作為屬性回調

1.申明block屬性

@property (copy, nonatomic) void(^chooseBlock)(NSInteger index);

2.在本類裡面調用

 if (_chooseBlock) {        _chooseBlock(self.Id);    }

3.使用block屬性:

  [cell setChooseBlock:^(NSInteger index) {        _cellIndex = index;        [_tableView reloadData];    }];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.