是否可以像在 HTML 单元格中一样在 iOS 中加入 UICollectionViewCells 内容可以加入 rowspan 或 colspan?
我有一个带有原型单元的UICollectionView.单元格加载并显示图像并显示标签.由于单元格具有不同的大小,因此可通过CollectionViewFlowLayout更改它们.这很好.
当我在模拟器中滚动视图时,标签似乎被重复使用并错误地添加到图像上.如何确保不会发生这种情况并且图像在集合视图上只有一个标签?

UICollectionView
#pragma mark - Collection view
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
self.Data = [NSArray arrayWithObjects:@1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, nil];
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.magazineLayout.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
int item = [indexPath row];
mCell.backgroundColor = [UIColor lightGrayColor];
// Set Image
UIImage *image;
image = [UIImage imageNamed:@"testimage.png"];
mCell.imageView.image = image;
// Set Label
NSString *title …Run Code Online (Sandbox Code Playgroud) xcode objective-c uilabel uicollectionview uicollectionviewcell
我正在尝试选择一个单元格UICollectionView,它被选中,但在向下滚动时,它选择底部的其他单元格并向上滚动显示其他一些要选择的单元格.
以下是我正在使用的代码 didSelectItemAtIndexPath
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *newIndex = indexPath;
cell = (CustomCollectionCell *)[collectionView cellForItemAtIndexPath:newIndex];
NSString *strId = [[masterArray valueForKey:@"id"]objectAtIndex:indexPath.row];
NSString *tempIndexRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
NSLog(@"%@, %@,%d ,%@, %d", strId,tempIndexRow,cell.imageView.tag,[boolArray objectAtIndex:indexPath.row],indexPath.row);
if (strId && [[boolArray objectAtIndex:indexPath.row] isEqualToString:@"False"] && cell.imageView.tag == indexPath.row) {
cell.selectedImage.image = [UIImage imageNamed:@"select.png"];
[boolArray replaceObjectAtIndex:indexPath.row withObject:@"True"];
}
else{
cell.selectedImage.image = Nil;
[boolArray replaceObjectAtIndex:indexPath.row withObject:@"False"];
}
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次选择的

这是我向下滚动时得到的结果

谢谢
我正在开发一个具有集合视图的应用程序,集合视图单元格有4个按钮来调用不同的动作.每个链接指向不同的IBAction.我已经全面阅读了多个主题并决定使用故事板而不是以编程方式进行,因为"这可能是前进的方向".因此,在设置按钮时,我已将默认标签文本设置为按钮1,按钮2等.在代码中,在为索引路径提供单元格时,我正在以编程方式将按钮文本更改为某些内容.运行应用程序时,单元格会在出列时呈现正常,并且正确设置以编程方式设置的文本.但是,当我单击任何按钮时,将调用该操作,但该按钮的标签文本将重置为使用故事板设置的内容.
我已经尝试了所有内容来保留文本标签副本应该是什么.但无济于事.当我再次通过滚动集合视图使单元格出列并重新绘制视图时,该按钮将再次获取正确的文本.
任何人都可以详细说明为什么放置在收集单元格中的按钮的按钮文本将重置其文本,并且除非重新绘制视图,否则不允许设置它.还有任何想法如何解决这个问题.
万分感谢
我有一个问题是在我的集合视图中添加额外的单元格我已经更改了逻辑以使用数据模型来填充我的单元格
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictures.count + 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("images", forIndexPath: indexPath) as! ProfileImageCell
let picture = pictures[indexPath.item] as! Gallery
print(indexPath.item)
if indexPath.item == pictures.count {
cell.image.image = UIImage(named: "ProfilePlusImage")
} else {
cell.image.sd_setImageWithURL(NSURL(string: picture.fileUrl), placeholderImage: UIImage(named: "Placeholder"))
cell.imageId = picture.id.integerValue
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
我想这个问题就在这一行
让picture = pictures [indexPath.item]为!画廊
当我标记并标记时
cell.image.sd_setImageWithURL(NSURL(string:picture.fileUrl),placeholderImage:UIImage(named:"placeholder"))
它在第11位增加了额外的细胞.但是其他方式让我超越了界限
谁能帮助我?
我有 CollectionView 有多个动态单元格,foreach 单元格有按钮,它具有添加项目数的操作,这是我的简单代码:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if ids.count == 0
{
return 3
}else
{
return ids.count
}
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if ids.count == 0
{
let cell = myCollection.dequeueReusableCellWithReuseIdentifier("loadingItems", forIndexPath: indexPath)
return cell
}else
{
let cell =myCollection.dequeueReusableCellWithReuseIdentifier("cellProduct", forIndexPath: indexPath) as! productsCollectionViewCell
cell.addItems.addTarget(self, action: #selector(homeViewController.addItemsNumberToCart(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
这是添加项目的方法
func addItemsNumberToCart(sender:UIButton)
{
sender.setTitle("Added to cart", forState: UIControlState.Normal)
}
Run Code Online (Sandbox Code Playgroud)
这是我的 collectionViewCell 类 …
我可以通过使用手势识别器拖动它来重新排序iOS 9上的UICollectionViewCells,并简化新的iOS 9支持以进行重新排序.
public func beginInteractiveMovementForItemAtIndexPath(indexPath: NSIndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES
public func updateInteractiveMovementTargetPosition(targetPosition: CGPoint)
public func endInteractiveMovement()
public func cancelInteractiveMovement()
Run Code Online (Sandbox Code Playgroud)
我注意到当我开始拖动一个单元格时,它的中心变为触摸位置,我不喜欢这样.
如果我愿意的话,我希望能够将我的细胞拖到它的角落.
你知道怎么做吗?
非常感谢.
滚动我的UICollectionView. 它显示一个空白视图,其中没有任何单元格。
它奇怪地隐藏了我的手机或移除了我的手机,我不知道,但我找不到任何解决方案。
我已经尝试过这些解决方案:
第一:尝试创建自定义类UICollectionViewFlowLayout并按照所有不同的方法进行操作,但无法解决我的问题。
第二:尝试了以下方法来重新分配和重新计算我的单元格大小:
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
我遵循的链接:
问题 2:使用我自己的 UICollectionViewFlowLayout 子类滚动时 UICollectionView 项目消失
请指导我使用自定义单元格的简单集合视图有什么问题。
参考图片:
objective-c ios uicollectionview uicollectionviewcell uicollectionviewlayout
好的,我在处理集合视图时遇到了问题。这就是我想要实现的 - 1 个大的 MAIN 页眉和页脚,然后嵌套在里面,需要一些数量(例如:4)个部分都只有 HEADERS。像这样:
------
tiny header 1
------
[a][b][c]
------
tiny header 2
------
[a][b][c]
Run Code Online (Sandbox Code Playgroud)
查看类似的答案,我首先尝试创建不同的小节,因为现在我只有所有集合视图单元格以及一个大页脚和页眉。问题是不管我有什么,我只剩下 1 节:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 4
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么。我从情节提要中获取我的收藏视图并将其设置如下:
@IBOutlet var collectionView: UICollectionView!
self.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //-44
self.collectionView.backgroundColor = UIColor.white
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerCell", for: indexPath as …Run Code Online (Sandbox Code Playgroud) 我不明白为什么我的细胞没有显示:
class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
let cellId = "pouet"
lazy var customCollectionView : UICollectionView = {
let layout = UICollectionViewLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.gray
cv.dataSource = self
cv.delegate = self
return cv
}()
override init(frame: CGRect) {
super.init(frame: frame)
customCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
addSubview(customCollectionView)
addConstraintsWithFormat(format: "H:|[v0]|", views: customCollectionView)
addConstraintsWithFormat(format: "V:|[v0]|", views: customCollectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: …Run Code Online (Sandbox Code Playgroud)