cell中互顶的效果实现 weico+

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

UITableView * tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];

    tv.delegate = self;

    tv.dataSource = self;

    [self.view addSubview:tv];

    [tv release];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 10;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString * cellID = [NSString stringWithFormat:@"cell_%d",indexPath.row];

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];

    if(!cell)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

        [self makeCell:cell atIndexPath:indexPath];

        [cell autorelease];

    }

    

    NSLog(@"tableView.contentOffset : %f",tableView.contentOffset.y);

    return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 150.0;

}


- (void)makeCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath

{

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 30)];

    label.backgroundColor = [UIColor orangeColor];

    label.text = [NSString stringWithFormat:@"%d",indexPath.row];

    [cell addSubview:label];

    [label release];

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    //scrollView转换为tableView

    UITableView * tv = (UITableView *)scrollView;

    NSIndexPath * indexPath = [tv indexPathForRowAtPoint:tv.contentOffset];

    //拿到当前在视图最顶部的cell

    UITableViewCell * tempCell = [tv cellForRowAtIndexPath:indexPath];

    //拿到所有可视化的cell

    NSArray * visibleCellArray = [tv visibleCells];

    UITableViewCell * firstCell = [visibleCellArray objectAtIndex:0];

    //如果这两个cell相等的话

    if(tempCell == firstCell)

    {

        //将视图中所有的cell中的label.frame.origin都置为0

        for(UITableViewCell * cell in visibleCellArray)

        {

            for(UIView * view in [cell subviews])

            {

                if(view.frame.size.height == 30)

                {

                    view.frame = CGRectMake(0, 0, 80, 30);

                }

            }

        }

    }

    //对当前视图最顶层的cell进行操作。

    NSArray * array = [tempCell subviews];

    NSInteger rowHeight;

    if(indexPath.row == 0)

    {

        //第一行 之前的行高为0

        rowHeight = 0;

    }

    else

    {

        // 之后的行 行高为之前行的总和

        rowHeight = 150 * indexPath.row;

    }

    //如果视图偏移量小于0 将最顶层的cell中的label的起点复位

    if(tv.contentOffset.y < 0)

    {

        NSArray * firstScreenCellArray = [tv visibleCells];

        UITableViewCell * firstCell = [firstScreenCellArray objectAtIndex:0];

        for(UIView * view in [firstCell subviews])

        {

            if(view.frame.size.height == 30)

            {

                view.frame = CGRectMake(0, 0, 80, 30);

            }

        }

    }

    for(UIView * view in  array)

    {

        if(view.frame.size.height == 30)

        {

            view.frame = CGRectMake(0, tv.contentOffset.y - rowHeight, 80, 30);

            if(tv.contentOffset.y + 30 > rowHeight + 150)

            {

                view.frame = CGRectMake(0, 120, 80, 30);

            }

            

        }

    }


    NSLog(@"%@",indexPath);

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

你可能感兴趣的:(cell中互顶的效果实现 weico+)