这两天遇到一个问题,UITableView中需要加入动画,而且每一行的速度不一样。

刚开始做时把所有的cell都遍历一遍加上动画,后来发现,如果数据很多时,就会出现各种各样的问题,而且没有显示在界面上的cell就没必要再用动画了,毕竟看不到。

后来发现UITableView中有这么一个方法:该方法是获取界面上能显示出来了cell。

- (NSArray *)visibleCells;

visible可见的。在当前页面中能看到cells都在这个数组中。

这样,我就根据这个数据来依次来遍历:

首先看一下这个Demo的效果:

http://my.csdn.net/my/album/detail/1718119

UITableView中的visibleCells的用法(visibleCells帮上大忙了)-LMLPHP

主要代码

 #pragma UITableView

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
} - (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];
cell.selectionStyle = UITableViewCellSelectionStyleNone; } cell.textLabel.text = [NSString stringWithFormat:@"visibleCell:%d",indexPath.row]; return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//点击效果
} - (IBAction)btnAction:(id)sender {
//获取可见cells
visibleCells = visibleTableView.visibleCells;
NSLog(@"%@",visibleCells); UIButton *button = (UIButton*)sender;
CGAffineTransform transform;
double duration = 0.2; if (button.tag == ) {
transform = CGAffineTransformMakeTranslation(-, );
}else if(button.tag == ){
for (UITableViewCell *cell in visibleCells) { [UIView animateWithDuration:duration delay: options: animations:^
{
cell.transform = CGAffineTransformIdentity; } completion:^(BOOL finished)
{ }];
duration+=0.1;
}
return;
}else{
transform = CGAffineTransformMakeTranslation(, ); }
for (UITableViewCell *cell in visibleCells) { [UIView animateWithDuration:duration delay: options: animations:^
{
cell.transform = transform; } completion:^(BOOL finished)
{ }];
duration+=0.1; } }
05-11 11:25