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]; }];