首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewCell重用

UICollectionViewCell重用
EN

Stack Overflow用户
提问于 2014-03-11 16:45:40
回答 3查看 4K关注 0票数 1

当方法重用时,有没有像这样的代码可以与UICollectionViewCell一起使用?

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

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

    if (!cell) 
    {
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId] autorelease];
    }

    return cell;
}
EN

回答 3

Stack Overflow用户

发布于 2014-03-11 16:55:14

整个要点是重用单元格,这就是为什么所有单元格的重用标识符应该是相同的,至少是一个类的所有单元格(这就是为什么将CellId声明为静态变量是有意义的-这个方法将被调用很多次)。dequeueReusableCellWithReuseIdentifier:方法返回准备好重用的单元格(如果有)。如果没有这样的单元格,你应该创建它,当它不再可见时,UICollectoinView会将它添加到“可重用单元池”中,并为dequeueReusableCellWithReuseIdentifier:返回。

代码语言:javascript
运行
复制
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath    {

    static NSString *CellId = @"YourCellIdentifier";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellId];
    if (!cell) {
        cell = [[CustomCell alloc] initWithFrame:yourFrame];
    }

    return cell;
}
票数 1
EN

Stack Overflow用户

发布于 2014-03-11 18:24:21

是的,collectionview也有类似的方法,如下所示:

代码语言:javascript
运行
复制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"collectionCell";

    collectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    return cell;
}
票数 0
EN

Stack Overflow用户

发布于 2014-03-11 18:30:32

是的,有:

代码语言:javascript
运行
复制
UICollectionReusableView *collView = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

您可以在文档中找到相关信息:https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/dequeueReusableCellWithReuseIdentifier:forIndexPath

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22320487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档