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

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

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

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

- (NSArray *)visibleCells;

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

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

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

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




主要代码:

#pragma UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}

- (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 == 1) {
        transform = CGAffineTransformMakeTranslation(-320, 0);
    }else if(button.tag == 2){
        for (UITableViewCell *cell in visibleCells) {
            
            [UIView animateWithDuration:duration delay:0 options:0  animations:^
             {
                 cell.transform = CGAffineTransformIdentity;

                 
             } completion:^(BOOL finished)
             {
                 
             }];
             duration+=0.1;
        }
        return;
    }else{
        transform = CGAffineTransformMakeTranslation(320, 0);

    }
    for (UITableViewCell *cell in visibleCells) {
        
        [UIView animateWithDuration:duration delay:0 options:0  animations:^
         {
             cell.transform = transform;
             
         } completion:^(BOOL finished)
         {
             
         }];
        duration+=0.1;
        
    }
    
}


另外如何把视频转成gif动画,请参考文章:"Mac OS X"录屏幕视频并转成gif

嘻嘻!^_^


猜你喜欢

转载自blog.csdn.net/rhljiayou/article/details/13634591