我正在实现一个自定义的UICollectionViewCell,我想知道如何向它发送模型数据,以便数据可用于初始化单元的子视图.
我通过做注册我的MyCollectionViewCell
[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
Run Code Online (Sandbox Code Playgroud)
在以下方法中,我做...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCollectionViewCell* cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
我面临的问题是,当一个单元不在重用队列中时,它通过调用initWithFrame方法(文档确认应该发生)来初始化一个新单元.但是,我的MyCollectionViewCell类中有一个自定义的init方法,它是......
-(id) initWithFrame:(CGRect)frame withData: (NSDictionary*) data;
Run Code Online (Sandbox Code Playgroud)
我希望调用自定义初始化方法.我该怎么办?我没有使用任何笔尖,我正在以编程方式执行所有操作.
如果没有办法,我必须使用initWithFrame方法,我还能如何将模型数据传递给MyCollectionViewCell,以便我可以用该数据初始化子视图?
谢谢!
我在我的iPad应用程序中使用collectionView.我使用了继承UICollectionViewFlowLayout类的自定义布局类.
我正在使用水平滚动方向.问题是,每当我们使用滚动进行集合视图时,有时候单元格会消失.我调试了代码,发现在创建单元格后的数据源方法中,其隐藏属性自动获得ON.
我尝试使用下面的线,但它无法正常工作
cell.hidden = NO
Run Code Online (Sandbox Code Playgroud)
我在下面的方法也使绑定更改的布局无效.
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但我仍面临着问题.对此有何解决方案?
提前致谢.
我试图简单地将UIView添加到占据整个单元框架的UICollectionViewCell.我现在的代码只在左上角显示一个单元格(我怀疑每个单元格在彼此的顶部进行分层).我怎么能这样做?
我计划稍后再继承UIView,以便自定义我添加到单元格的视图.我不想基于我将使用它的方式继承UICollectionViewCell.
#pragma mark - Collection view data source
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 6;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
UIView *menuItem = [[UIView alloc] init];
menuItem.frame = cell.frame;
menuItem.backgroundColor = [UIColor greenColor];
[cell addSubview:menuItem];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; {
return CGSizeMake(60, 60);
}
Run Code Online (Sandbox Code Playgroud) 我有类似的问题,这样的
我在运行时生成视图高度.这是我的代码
@interface CollectionViewController ()
{
NSMutableArray *arrMain;
}
@property(nonatomic,strong) NSMutableArray *arrMain;
@end
@implementation CollectionViewController
@synthesize arrMain,
- (void)viewDidLoad
{
[super viewDidLoad];
[cView registerNib:[UINib nibWithNibName:@"CViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
CViewFlowLayout *fl = [[CViewFlowLayout alloc] init];
self.cView.collectionViewLayout = fl;
NSString *strJson = MY FUNCTION WHICH RETURNS JSON STRING;
SBJSON *parser = [[SBJSON alloc] init];
self.arrMain = [[NSMutableArray alloc] init];
self.arrMain = [parser objectWithString:strJson error:nil];
for (int i=0; i<[self.arrMain count]; i++) {
NSDictionary *dic = [self.arrMain objectAtIndex:i];
[self setTags:[[UIView alloc] init] selDictionary:dic]; // …Run Code Online (Sandbox Code Playgroud) 我UICollectionView用来显示与服务器相关的信息.这UICollectionView允许单元格选择显示一些嵌套数据.不幸的是,如果用户在我的应用程序调用期间触摸并保留任何单元[collectionView reloadData]格,则此单元格不再对触摸作出反应(collectionView:didSelectItemAtIndexPath:方法未被调用).我可以选择除此之外的任何细胞.
我创建了可以重现此问题的简单应用程序:链接
任何想法如何解决它?
在删除一个我得到断言UICollecitonViewCell从UICollectionView.
前提条件:我有一个单元格(当我有两个或更多单元格时,删除效果很好).
这是代码:
NSIndexPath *ip = [_photosCollectionView indexPathForCell:cell];
[_datasource removeItemAtIndex:ip.item];
[_photosCollectionView deleteItemsAtIndexPaths:@[ip]]; // the assertion is raised
Run Code Online (Sandbox Code Playgroud)
这是断言文本:
NSInternalInconsistencyException: attempt to delete item 0 from section 0 which only contains 0 items before the update
Run Code Online (Sandbox Code Playgroud)
非常奇怪的问题,因为它适用于2,3或更多单元格,但是当我删除单个单元格时,它会失败.
任何想法有什么不对,如何解决这个问题?
我想知道如何遍历当前可见的所有CollectionView单元格.
在Objective C中,我将实现以下概念:
for(UICollectionView *cell in collectionView.visibleCells){
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试将其更改为swift:
for cell:MyCollectionViewCell in self.collectionView.visibleCells() as cell:MyCollectionViewCell {
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
Type 'MyCollectionViewCell' does not conform to protocol 'SequenceType'
Run Code Online (Sandbox Code Playgroud)
如何遍历所有CollectionViewCells
我有一个CollectionView向用户显示图像.我在后台下载这些,当下载完成后,我调用以下func来更新collectionViewCell并显示图像.
func handlePhotoDownloadCompletion(notification : NSNotification) {
let userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!>
let id = userInfo["id"]
let index = users_cities.indexOf({$0.id == id})
if index != nil {
let indexPath = NSIndexPath(forRow: index!, inSection: 0)
let cell = followedCollectionView.cellForItemAtIndexPath(indexPath) as! FeaturedCitiesCollectionViewCell
if (users_cities[index!].image != nil) {
cell.backgroundImageView.image = users_cities[index!].image!
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果单元格当前在屏幕上可见,则此方法很有效,但如果不是
fatal error: unexpectedly found nil while unwrapping an Optional value,则在以下行中出现错误:
let cell = followedCollectionView.cellForItemAtIndexPath(indexPath) as! FeaturedCitiesCollectionViewCell
Run Code Online (Sandbox Code Playgroud)
现在,如果collectionViewCell尚未可见,则甚至不需要调用此函数,因为在这种情况下,cellForItemAtIndexPath无论如何都将在方法中设置图像.
因此我的问题是,如何更改此函数以检查我们正在处理的单元格当前是否可见.我知道collectionView.visibleCells()但是,我不知道如何在这里应用它.
我想在集合视图中重新排序每个单元格的自定义大小的单元格.
在Collection View的每个单元格中都有一个带有单词的标签.
我用这段代码设置每个单元格的维度:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let word = textArray[indexPath.row]
let font = UIFont.systemFont(ofSize: 17)
let fontAttributes = [NSFontAttributeName: font]
var size = (word as NSString).size(attributes: fontAttributes)
size.width = size.width + 2
return size
}
Run Code Online (Sandbox Code Playgroud)
我使用以下代码重新排序集合视图:
override func viewDidLoad() {
super.viewDidLoad()
self.installsStandardGestureForInteractiveMovement = false
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(gesture:)))
self.collectionView?.addGestureRecognizer(panGesture)
}
func handlePanGesture(gesture: UIPanGestureRecognizer) {
switch gesture.state {
case UIGestureRecognizerState.began :
guard let selectedIndexPath = self.collectionView?.indexPathForItem(at: gesture.location(in: …Run Code Online (Sandbox Code Playgroud) 如何轻松地创建一个水平滚动集合视图,填充单元格跨越行而不是列向下?
我希望有5列和3行,但当有超过15项时,我希望它滚动到下一页.
这件事我有很多麻烦.
horizontal-scrolling ios uicollectionview uicollectionviewcell swift