我知道UICollectionViewDelegateFlowLayout是协议,而UICollectionViewFlowLayout是类,并且我知道协议和类之间的区别。据我了解,我可以拥有一个遵循UICollectionViewDelegateFlowLayout协议的类,以实现与继承自UICollectionViewFlowLayout的类完全相同的效果。我通过找到协议和类之间的这种关系来得出这个结论:UICollectionViewDelegateFlowLayout ------------- UICollectionViewFlowLayout collectionView:layout:sizeForItemAtIndexPath:---- itemSize collectionView:layout:insetForSectionAtIndex:--- -sectionInset – collectionView:layout:referenceSizeForHeaderInSection:-headerReferenceSize – collectionView:layout:referenceSizeForFooterInSection:-footerReferenceSize
我还在该协议的参考中阅读了以下内容:“该协议中的所有方法都是可选的。如果您未实现特定方法,则流布局委托将在其自己的属性中使用值来获取适当的间距信息”理解是:如果CollectionView具有布局属性和委托流布局,则委托流布局可能会覆盖某些内容。换句话说,我可以同时拥有该协议和更高的优先级。对?
那么,发明能做同样事情的协议和类背后的逻辑是什么?
ios uicollectionview uicollectionviewlayout uicollectionviewdelegate
我已经将协议符合UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate一个单独的类,CollectionViewWeekDelegate但didSelectItemAt没有调用诸如此类的委托方法。
我委托也设置CollectionView在viewDidLoad()
请在下面找到代码:
委托类
class CollectionViewWeekDelegate: NSObject, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
internal var parent: EarningsViewController?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
parent?.weekTabSelectedPosition = indexPath.item
let indexPathh = IndexPath(item: indexPath.item, section: 0)
parent?.collectionViewSchedule.scrollToItem(at: indexPathh, at: .right, animated: true)
parent?.collectionViewWeeks.scrollToItem(at: indexPath, at: .top, animated: true)
parent?.collectionViewWeeks.reloadData()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let offsetX = parent?.collectionViewSchedule.contentOffset.x
let contentWidth = parent?.collectionViewSchedule.bounds.width
let page = Int(offsetX! / contentWidth!)
parent?.weekTabSelectedPosition = page …Run Code Online (Sandbox Code Playgroud) 如何在UICollectionView的一部分中设置单元格间距?UICollectionView有一个属性minimumInteritemSpacing,我需要设置1.0仍然无法正常工作.我实现了委托方法.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1.0;
}
Run Code Online (Sandbox Code Playgroud) objective-c ios uicollectionviewcell uicollectionviewdelegate
我有一个,UICollectionViewController并且此集合的单元格有一个子视图-假设A-与单元格边界相同。
问题是当我点击这些单元格时,collectionView:didSelectItemAtIndexPath不会被调用。我还将A的ExclusiveTouch设置为YES,但仍然没有执行任何操作。而且,当我将A的框架更改为较小的尺寸时,点击剩余区域会触发效果collectionView:didSelectItemAtIndexPath,这没问题。
那时候我应该怎么做才能使这个collectionView委托在A的界限与单元格相同的地方被调用。
我将不胜感激。
objective-c ios uicollectionview uicollectionviewcell uicollectionviewdelegate
在下面的代码中,collectionView将平滑滚动,但有时如果我滚动速度非常快,则会显示一个不正确的图像,然后在滚动减速时更改为正确的图像.为什么不设置下面是我正在使用的 代码我的代码:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell;
cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
NSMutableArray *arr_assect=[menuArray objectAtIndex:indexPath.row];
backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
UIImageView *dotlines=[[UIImageView alloc]init];
dotlines.frame=CGRectMake(1, 0, cell.bounds.size.width-1, cell.bounds.size.height);
dotlines.image=[UIImage imageNamed:@"dottedline"];
[cell addSubview:dotlines];
UIImageView*cellImage=[[UIImageView alloc]initWithFrame:CGRectMake(18, 10,backView.bounds.size.height-35, backView.bounds.size.height-40)];
NSLog(@"arrayimage%@",[[menuArray valueForKey:@"category_image"] objectAtIndex:indexPath.row]);
NSString *str_homepage = [NSString stringWithFormat:@"%@",[[menuArray valueForKey:@"home_page"] objectAtIndex:indexPath.row]];
NSLog(@"str_home_page%@",str_homepage);
NSString *imageUrl= [arr_assect valueForKey:@"category_image"];
if ([[ImageCache sharedImageCache] DoesExist:imageUrl]) {
cellImage.image=[UIImage imageWithData:[[ImageCache sharedImageCache] GetImage:imageUrl]];
}
else{
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
cellImage.image=[UIImage …Run Code Online (Sandbox Code Playgroud) objective-c ios uicollectionview uicollectionviewcell uicollectionviewdelegate
我有UICollectionView委托的问题.因为insetForSectionAt委托从不调用.
所以请看这个代码,并帮我解决这个问题:
import UIKit
class GalleryCollectionViewController: UICollectionViewController {
var dataSourceArr:Array<UIImage>!
override init(collectionViewLayout layout: UICollectionViewLayout) {
super.init(collectionViewLayout: layout)
collectionView?.collectionViewLayout = layout
collectionView!.register(GalleryCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if dataSourceArr.count != 0 {
return dataSourceArr.count
}
return 0
}
override func collectionView(_ …Run Code Online (Sandbox Code Playgroud) delegates ios uicollectionview uicollectionviewdelegate swift
如何在DidSelectItemAtIndexPath上获取集合视图的Header,以便在选择项目时更改header的文本。
我以编程方式构建了一个 UICollectionViewController,它返回 4 个单元格。HomeCell、TrendingCell、SubscriptionCell 和 AccountCell。所有 4 个单元格应该不同,您可以水平滚动它们。<--->。
class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout{
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.register(HomeCell.self, forCellWithReuseIdentifier: homeCellId)
collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId)
collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId)
collectionView?.register(AccountCell.self, forCellWithReuseIdentifier: accountCellId)
}
}Run Code Online (Sandbox Code Playgroud)
让我们以 HomeController 的第一个单元(称为 HomeCell)来说明我的问题。Homecell 具有三个自定义单元,称为 VideoCell、CategoryCell 和 UserSearchCell。
class HomeCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
let searchId = "sarchId"
let scrollId = "scrollId"
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
cv.dataSource = …Run Code Online (Sandbox Code Playgroud)uinavigationcontroller ios uicollectionviewcell uicollectionviewdelegate swift