我正在做一个类似于视频专辑的项目。我UICollectionView用来显示这些视频的拇指图像。最糟糕的是我不应该使用故事板或 xib 文件。我试图以编程方式做到这一点。我目前正在处理此代码:
- (void)viewDidLoad
{
i = 0;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setItemSize:CGSizeMake(self.view.frame.size.width/2.5,self.view.frame.size.width/2.5)];
collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
collectionView.backgroundColor = [UIColor whiteColor];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
[self.view addSubview:collectionView];
[super viewDidLoad];
}
Run Code Online (Sandbox Code Playgroud)
我已经给了 1 以返回numberOfSectionsInCollectionView和[myArray count]返回numberOfItemsInSection.
-(UICollectionViewCell *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - …Run Code Online (Sandbox Code Playgroud) 我正在设置一个项目,UICollectionView其中显示带有标签的图像.我创造了ViewController和CellViewController它一样,就像它应该的那样.
在CellViewController代码中如下:
class CellController: UICollectionViewCell {
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var label: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
但是ViewController当我设置图像时,它给了我错误.这是代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCell = collection.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellController
cell.image.image = UIImage(named: "star")
return cell
}
Run Code Online (Sandbox Code Playgroud)
最奇怪的是,我有一个从GitHub下载的项目,它与我的基本相同,而且工作正常.
提前致谢!
我正在尝试UICollectionView根据这个主题构建一个标签流布局:http : //codentrick.com/create-a-tag-flow-layout-with-uicollectionview/
它一直工作到按钮动作,但有一个问题。正如您在下图中所看到的, customUICollectionViewCell不像主题那样正确对齐。我做的一切都一样,但是FlowLayout.swift
我想在这些单元格之间留出相等的间隙。但他们是这样对齐的。
你可以在FlowLayout.swift下面找到。
有人能告诉我我该如何解决吗?
import Foundation
import UIKit
class FlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributesForElementsInRect = super.layoutAttributesForElementsInRect(rect)
var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
var leftMargin : CGFloat = 0.0
for attributes in attributesForElementsInRect! {
let refAttributes = attributes
// assign value if next row
if (refAttributes.frame.origin.x == self.sectionInset.left) {
leftMargin = self.sectionInset.left
} else {
// set x position of attributes to current margin …Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
我正在使用 Alamofire 从网络服务获取图像。我使用以下代码在 UICollectionView 的自定义单元格中显示这些图像
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let reuseIdentifier = "cell"
// get a reference to our storyboard cell
let cell : ImageCollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellCollection", forIndexPath: indexPath) as! ImageCollectionCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.backgroundColor = UIColor.yellowColor() // make cell more visible in our example project
let imageGalleryURLString = self.MutableArray.objectAtIndex(indexPath.row) .valueForKey("thumbnail") as! String
let imageURL = NSURL(string:imageGalleryURLString)
cell.imageviewCollectionCell.sd_setImageWithURL(imageURL)
return cell …Run Code Online (Sandbox Code Playgroud) 我有一个UICollectionView带有自定义的水平UICollectionViewCell。每个单元格都有一个UIImageView,显示用户选择的图像。现在,我想做的是在每个单元格的角落添加一个删除按钮(或图像?),当单击它时,它会从数组和单元格本身中删除相应的图像。我有两个问题:
UIViewController?这
像这样的东西:
这是我的自定义单元格类:
class PhotoCell: UICollectionViewCell, UIGestureRecognizerDelegate
{
@IBOutlet weak var cellImage: UIImageView!
override func awakeFromNib()
{
super.awakeFromNib()
setupUI()
}
func setupUI()
{
cellImage.layer.cornerRadius = 5
cellImage.clipsToBounds = true
cellImage.contentMode = .scaleAspectFill
}
}
Run Code Online (Sandbox Code Playgroud)
这是整个View Controller的代码
import UIKit
import SDWebImage
import Photos
import AssetsPickerViewController
import SVProgressHUD
class PhotoVC: UIViewController
{
// MARK: IBOutlets
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var postButton: UIButton!
@IBOutlet weak var userProfileImage: UIImageView!
@IBOutlet …Run Code Online (Sandbox Code Playgroud) 如果在索引 path.item 的单元格中满足某些条件,我想访问集合视图中的前一个单元格(索引 path.item - 1)以更改那里的某些设置
现在我试过这个:
let previousCell = collectionView.cellForItem(at: indexPath - 1)
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为 - 1 它从索引路径向下转换为整数值。
我怎么得到这个细胞?
我有一个带有多个custom UICollectionViewCell的快捷应用程序。所有细胞都有多个对象(无论是UIImageView或UILabel),用户可以点击(因此,我使用多个UITapGestureRecognizers到调用相应的行动。现在,为了减轻我的工作,使代码少重复,我想创造一个extension的UITapGestureRecognizer用static我可以直接在类上调用的方法。
我确实做到了,但是应用程序崩溃的事实意味着有些事情我做得不好。这是我的代码:
extension UITapGestureRecognizer {
static func addNewTapGuestureRecognizer(for imageView: UIImageView, selectorName: Selector) {
let tap = UITapGestureRecognizer(target: self, action: selectorName)
imageView.addGestureRecognizer(tap)
imageView.isUserInteractionEnabled = true
}
static func addNewTapGuestureRecognizer(for label: UILabel, selectorName: Selector) {
let tap = UITapGestureRecognizer(target: self, action: selectorName)
label.addGestureRecognizer(tap)
label.isUserInteractionEnabled = true
}
}
class TextCVC: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
addTapGuesturesForImagesAndLabels()
}
func addTapGuesturesForImagesAndLabels() {
UITapGestureRecognizer.addNewTapGuestureRecognizer(for: postActionShareImageVIew, selectorName: #selector(self.shareImageTapped))
UITapGestureRecognizer.addNewTapGuestureRecognizer(for: postActionLikeImageVIew, …Run Code Online (Sandbox Code Playgroud) 我被困在我的项目中,需要一些帮助。我想UICollectionViewController从UICollectionViewCell 包括我的UINavigationBar. 我知道您实际上无法从 Cell 中呈现 ViewController?这种情况我该怎么办?我的问题是我的 NavigationBar 不会出现。
这是我的细胞:
import UIKit
import Firebase
class UserProfileHeader: UICollectionViewCell, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
lazy var followersLabelButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("followers", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(followers), for: .touchUpInside)
return button
}()
@objc fileprivate func followers() {
let newController = NewController(collectionViewLayout: UICollectionViewFlowLayout())
self.window?.rootViewController?.present(newController, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
和 CollectionViewController:
import UIKit
import MapKit
import Firebase
class …Run Code Online (Sandbox Code Playgroud) 我以编程方式构建了一个 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
通过将minimumLineSpacing集合视图布局的属性设置为负数,我让我的单元格重叠。但是,当我滚动并重绘单元格时,它们现在以相反的方向重叠。我在下面放了图片。
当滚动集合视图并重绘单元格时,如何保持单元格重叠,如第一张图片所示?
import UIKit
class PopularView: UIView {
let cellID = "cellID"
// MARK: - Views
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = -55 // -------Allows Overlap-----
layout.itemSize = CGSize(width: SCREEN_WIDTH, height: 185)
layout.minimumInteritemSpacing = 17
let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
view.backgroundColor = .white
return view
}()
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.dataSource = self
collectionView.register(PopularCell.self, forCellWithReuseIdentifier: cellID)
backgroundColor = .white
setupCollectionView()
}
required …Run Code Online (Sandbox Code Playgroud) ios uicollectionview uicollectionviewcell uicollectionviewlayout swift
您好,我想使用 uicollectionview 进行拖放。当执行拖放时,它正在移动内容,我想像照片中那样进行操作。我想让盒子自己搬。例如; 当我将照片拖动到 4 时,我应该保留带有完整测量值的红色区域。将照片 6 交换到照片 1,就像将照片 3 移到左侧一样。我在 uicollectionview 中研究了很多,但我无法\xe2\x80\x99 找到类似的东西。请帮我
\n\nimport UIKit\n\nfinal class ViewController: UIViewController {\n\n@IBOutlet weak var collectionView: UICollectionView!\n\n var cellIds = ["image 1","image 2","image 3","image 4","image 5","6","7"]\n override func viewDidLoad() {\n super.viewDidLoad()\n\n let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout\n flowLayout.estimatedItemSize = CGSize(width: 200, height: 10)\n\n let gestureRecognizer = UILongPressGestureRecognizer(target: self,\n action: #selector(self.handleLongPress(gestureRecognizer:)))\n collectionView.addGestureRecognizer(gestureRecognizer)\n}\n\n@objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) {\n\n guard let view = gestureRecognizer.view else { return }\n let location = gestureRecognizer.location(in: view)\n\n switch gestureRecognizer.state {\n …Run Code Online (Sandbox Code Playgroud) 我正在配置一个集合视图以使用我自己的自定义单元格,但是,我总是想知道为什么我必须在 collectionview.register(任何类,forCellReuseIdentifier)内的单元格类中包含 .self 。
也许我的 OOP 技能在这里很缺乏,但是为什么 .self 会引用类型对象呢?为什么需要知道类型?任何帮助,将不胜感激。
func configureCollectionView() {
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: UICollectionViewLayout())
view.addSubview(collectionView)
collectionView.backgroundColor = .systemPink
//collectionView.register(FollowerCell.self, forCellWithReuseIdentifier: FollowerCell.reuseID)
collectionView.register(FollowerCell, forCellWithReuseIdentifier: FollowerCell.reuseID)
}
Run Code Online (Sandbox Code Playgroud)
class FollowerCell: UICollectionViewCell {
static let reuseID = "FollowerCell"
let avatarImageView = GFAvatarImageView(frame: .zero)
let userNameLabel = GFTitleLabel(textAlignment: .center, fontSize: 16)
override init(frame: CGRect) {
super.init(frame: frame)
configure()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
Run Code Online (Sandbox Code Playgroud) ios ×11
swift ×11
uicollectionview ×10
objective-c ×1
swift2 ×1
swift3 ×1
uiimageview ×1
uikit ×1
xcode ×1