iOS  点击cell下拉

代码如下:

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, copy) NSMutableArray *muArr;
@property (nonatomic, copy) NSMutableArray *boolArray;
@property (nonatomic, strong) UITableView *tableView; @end @implementation ViewController
#pragma mark - LifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor];
NSArray *array1 = @[@"",@"",@"",@"",@""];
NSArray *array2 = @[@"a",@"b",@"c",@"d",@"e",@"f"];
[self.muArr addObject:array1];
[self.muArr addObject:array2]; for (int i = ; i < self.muArr.count; i++) {
[self.boolArray addObject:@"no"];
}
[self.view addSubview:self.tableView];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} #pragma mark - UITableViewDataSource and ITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.muArr.count;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//如果self.boolArray的内容为“yes”,返回self.muArr中某个数组的个数,否则返回2
if ([self.boolArray[section] isEqualToString:@"yes"]) {
return [[self.muArr objectAtIndex:section] count];
} else {
return ;
}
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"cellID";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
/*
else {
//如果不增加下面这个判断,tableview 再滑动后,会有重影,或者在被选中后会有重影
NSArray *views = [cell subviews];
for (UIView *obj in views) {
if (obj.tag==1000 || obj.tag==2000) {
NSLog(@"cell 要删除的子画面是:%@",[obj class]);
[obj removeFromSuperview];
}
}
}
*/
//显示每个section的第一个
if ([[self.muArr objectAtIndex:indexPath.section] count] > indexPath.row) {
cell.textLabel.text = [[self.muArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
}
//每个section的indexPath.row == 1显示为“更多”
if ([self.boolArray[indexPath.section] isEqualToString:@"no"] && indexPath.row == &&
[[self.muArr objectAtIndex:indexPath.section] count] != ) {
cell.textLabel.text = nil;
cell.textLabel.text = @"更多";
}
return cell;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *array = [[NSMutableArray alloc] init];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow: inSection:indexPath.section]]; if ([self.boolArray[indexPath.section] isEqualToString:@"no"]) {
self.boolArray[indexPath.section] = @"yes";
cell.textLabel.text = [[self.muArr objectAtIndex:indexPath.section] objectAtIndex:];
for (NSInteger i = ; i < [[self.muArr objectAtIndex:indexPath.section] count]; i++) {
NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[array addObject:index];
}
[self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}else{
self.boolArray[indexPath.section] = @"no";
cell.textLabel.text = @"更多";
for (NSInteger i = ; i < [[self.muArr objectAtIndex:indexPath.section] count]; i++) {
NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
[array addObject:index];
}
[self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
}
} - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[self.muArr objectAtIndex:indexPath.section] count] == ) {
return nil;
}
if ([self.boolArray[indexPath.section] isEqualToString:@"yes"]) {
return indexPath;
}else{
if (indexPath.row == ) {
return indexPath;
}else{
return nil;
}
}
} #pragma mark - Getter and Setter
- (NSMutableArray *)muArr {
if (!_muArr) {
_muArr = [[NSMutableArray alloc] init];
}
return _muArr;
} - (NSMutableArray *)boolArray {
if (!_boolArray) {
_boolArray = [[NSMutableArray alloc] init];
}
return _boolArray;
} - (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
}
return _tableView;
} @end
04-13 19:54