ITeye博客

cell重用问题

xiaotongeye · 2014-11-01 20:44 · 基础学习 · 编程语言 · Markdown

解决问题之前,在cell上添加了label,但是给label添加label.text之后滑动时即用到cell重用时label的内容总是重叠,从网上搜了好多,现在终于解决了,总结一下我用的方法:就是给重用的cell也设置不同的标示符。首先,对于tableviewcell


    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];
    if (cell == nil)
    {
        cell = [[uitableviewcell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

只需要在定义标示符的时候换成这句就可以了


NSString *CellIdentifier = [nsstring stringwithformat:@"cell%d",indexpath.row];

这样就能保证每一行的标示符都不一样,也就不会出现重用问题。 还有在collectionViewcell时用同样的方法解决


NSString *CellIdentifier = [nsstring stringwithformat:@"cell%d",indexpath.row];
    [m_collectionview registerclass:[uicollectionviewcell class] forCellWithReuseIdentifier:CellIdentifier];
        UICollectionViewCell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath];

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。