UITbaleView and uitbaleview

Source: Internet
Author: User

UITbaleView and uitbaleview

Set the Id attribute to mark the cell

@property (nonatomic,assign)NSInteger Id;

Set a button with different normal status and selected status.

_ Choose = [[UIButton alloc] init]; [_ choose setImage: [UIImage imageNamed: @ "about _ deselected"] forState: UIControlStateNormal]; [_ choose setImage: [UIImage imageNamed: @ "about _ Check"] 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);}

Set a method to adjust or select

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

Click Event on cell

/*** Select ** @ param sender **/-(void) choosePressed :( UIButton *) sender {sender. selected = YES; if (_ chooseBlock) {_ chooseBlock (self. id );}}

Set a global variable to determine which cell is located.

    NSInteger _cellIndex;

 

TableView Proxy:-(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;}

:

 

Supplement: block is used as the property callback.

1. Declare block attributes

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

2. Call

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

3. Use the block attribute:

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.