鹏哥写的一个小demo
效果图是这样的:
代码是:
#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableV;
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
@end
static NSString *const cellID = @"cellID";
@implementation ViewController
{
CustomCell *cell;
NSMutableDictionary *selectedIndexes;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableV.backgroundColor = [UIColor lightGrayColor];
selectedIndexes = [[NSMutableDictionary alloc] init];
[self.tableV registerNib:[UINib nibWithNibName:NSStringFromClass([CustomCell class]) bundle:nil] forCellReuseIdentifier:cellID];
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
//
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self cellIsSelected:indexPath]) {
return 60;
}
return 35;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
BOOL isSelected = ![self cellIsSelected:indexPath];
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
[self.tableV beginUpdates];
[self.tableV endUpdates];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell = [tableView dequeueReusableCellWithIdentifier:cellID];
cell.button.tag = 100+indexPath.row;
cell.button.backgroundColor = [UIColor redColor];
[cell.button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)btnClick:(UIButton *)sender{
}
@end
8455

被折叠的 条评论
为什么被折叠?



