`
dcj3sjt126com
  • 浏览: 1825420 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

NsTimer 和 UITableViewCell 之间的控制

    博客分类:
  • IOS
ios 
阅读更多

情况是这样的:

一个UITableView, 每个Cell的内容是我自定义的 viewA viewA上面有很多的动画, 我需要添加NSTimer来做动画, 由于TableView的复用机制, 我添加的动画会不断开启, 没有停止, 动画会执行越来越多.

 

解决办法:

在配置cell的时候开始动画, 然后在cell结束显示的时候停止动画

 

查找cell结束显示的代理方法, 打开dash 在ios分类中搜索 uitableviewdelegate, 找找所有的代理方法, 发现在Tracking the Removal of Views这个类别中有一个代理方法叫: - tableview:didEndDisplayingCell:forRowAtIndexPath: 这个方法就是我们所需要的, 在个方法里面调用停止动画的方法即可

 

关键代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RecentlyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Feed * feed = (Feed *)self.data[indexPath.row];
    cellviewA.feed = feed;
    [cell.stageView performSelector:@selector(startAnimation) withObject:nil afterDelay:1];
    return cell;
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    RecentlyCell * recentlyCell = (RecentlyCell *)cell;
    [recentlyCell.viewA stopAnimation];
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics