当方法重用时,有没有像这样的代码可以与UICollectionViewCell一起使用?
- (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;
}
发布于 2014-03-11 16:55:14
整个要点是重用单元格,这就是为什么所有单元格的重用标识符应该是相同的,至少是一个类的所有单元格(这就是为什么将CellId
声明为静态变量是有意义的-这个方法将被调用很多次)。dequeueReusableCellWithReuseIdentifier:
方法返回准备好重用的单元格(如果有)。如果没有这样的单元格,你应该创建它,当它不再可见时,UICollectoinView
会将它添加到“可重用单元池”中,并为dequeueReusableCellWithReuseIdentifier:
返回。
- (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;
}
发布于 2014-03-11 18:24:21
是的,collectionview也有类似的方法,如下所示:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"collectionCell";
collectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
发布于 2014-03-11 18:30:32
是的,有:
UICollectionReusableView *collView = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
https://stackoverflow.com/questions/22320487
复制相似问题