tableView点击下拉更多的效果

tableView点击下拉查看更多,主要原理就是在tableView的didSelect方法里插入(更多)或者删除(回收)一定数量的cell。

本例子是用section作为单位来分块。个人感觉这种方式是最方便的。

其中用到了一个二维数组(数据源),一个一维数组,用于存放每个section的状态。

头文件如下:

#import <UIKit/UIKit.h>

@interface MainViewController : UITableViewController{
    NSMutableArray *_nameArray;
    NSMutableArray *boolArray;
}

@property (nonatomic, retain) NSMutableArray *nameArray;

@end


实现文件如下:

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation FNDMainViewController
//@synthesize nameArray = _nameArray;

#pragma mark - lifeCycle methods
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        _nameArray = [[NSMutableArray alloc] init];
        NSArray *array1 = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        NSArray *array2 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g", nil];
        NSArray *array3 = [NSArray arrayWithObjects:@"liu",@"ping",@"wei",@"ni",@"hao",@"haha",@"your", nil];
        NSArray *array4 = [NSArray arrayWithObjects:@"apple",@"banana",@"orange",@"give",@"me",@"off",@"up", nil];
        /*
        NSArray *array5 = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
        NSArray *array6 = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g", nil];
        NSArray *array7 = [NSArray arrayWithObjects:@"liu",@"ping",@"wei",@"ni",@"hao",@"haha",@"your", nil];
        NSArray *array8 = [NSArray arrayWithObjects:@"apple",@"banana",@"orange",@"give",@"me",@"off",@"up", nil];
        NSArray *array9 = [NSArray arrayWithObjects:@"you",@"me", nil];
         */
        
        [_nameArray addObject:array1];
        [_nameArray addObject:array2];
        [_nameArray addObject:array3];
        [_nameArray addObject:array4];
        /*
        [_nameArray addObject:array5];
        [_nameArray addObject:array6];
        [_nameArray addObject:array7];
        [_nameArray addObject:array8];
        [_nameArray addObject:array9];
         */

    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];
    boolArray = [[NSMutableArray alloc] init];
    //YES为拉伸  NO为收缩
    for (int i = 0; i < self.nameArray.count; i++) {
        [boolArray addObject:@"no"];
    }
}

#pragma mark - manage memory metohds

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)dealloc{
    boolArray = nil;
    [boolArray release];
    _nameArray = nil;
    [_nameArray release];
    [super dealloc];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //return [_nameArray count];
    return self.nameArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([boolArray[section] isEqualToString: @"yes"]) {
        return [[_nameArray objectAtIndex:section] count];
    }else{
        return 2;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    //如果本section对应的数组成员数大于本section的行数,则说明是收缩状态
    if ([[_nameArray objectAtIndex:indexPath.section] count] > indexPath.row) {
        cell.textLabel.text = [[_nameArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }
    //如果本section对应的状态为no,并且本行为本section的第二行,并且本section对应的内容数组成员不等于2则说 明是收缩状态,将第二行的textLabel改为@"更多"
    if ([boolArray[indexPath.section] isEqualToString: @"no"] &&indexPath.row == 1 &&
        [[self.nameArray objectAtIndex:indexPath.section] count] != 2) {
        cell.textLabel.text = nil;
        cell.textLabel.text = @"更多";
    }
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //改变选择的状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableArray *array = [[NSMutableArray alloc] init];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:indexPath.section]];
    //如果选中行所在的section为收缩状态
    if ([boolArray[indexPath.section] isEqualToString: @"no"]) {
        //改变状态值
        boolArray[indexPath.section] = @"yes";
        //把更多修改为数组里对应的值
        cell.textLabel.text = [[self.nameArray objectAtIndex:indexPath.section]objectAtIndex:1];
        //关键部分
        for (int i = 2; i < [[_nameArray objectAtIndex:indexPath.section] count]; i++) {
            NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [array addObject:index];
        }
        
        [self.tableView insertRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade];
        
    }else{
        //如果为拉伸状态,把状态改为收缩
        boolArray[indexPath.section] = @"no";
        //把第二行改为@“更多”
        cell.textLabel.text = @"More";
        for (int i = 2; i < [[_nameArray objectAtIndex:indexPath.section] count]; i++){
            NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [array addObject:index];
        }
        [self.tableView deleteRowsAtIndexPaths:array                          withRowAnimation:UITableViewRowAnimationFade];
    }
    [array release];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //如果这个section的cell只有2个,丫的返回nil
    if ([[self.nameArray objectAtIndex:indexPath.section] count] == 2) {
        return nil;
    }
    //判断是否为收缩状态
    if ([boolArray[indexPath.section] isEqualToString: @"yes"]) {
            return indexPath;
    }else{
        //如果为收缩状态,判断点击的行数是否为第二行(更多),如果不是点第二行(更多),则返回nil
        if (indexPath.row == 1) {
            return indexPath;
        }else{
            //NSLog(@"收缩状态,不是点更多");
            return nil;
        }
    }
    
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值