I have a collectionView at the top of my App view and I want to display some ads inside it,
I made a ready array for some pictures that I want to display them in the collection view.
Now I'm looking for a method that I can make them horizontally scroll automatically.
class HomeVC: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionViewCell()
}
#IBOutlet weak var collectionView: UICollectionView!
var array = [ "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcR8aTilga7j-8GfP4OUXUx1bV3E2EJaFt29QdSBD8OgcLBLCUiG&usqp=CAU?",
"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRVq4AqZIDQ_UPnSd-PxQGkEutZnlu76NbZ4xETWelVnULLJ614&usqp=CAU",
"https://scontent.ffjr1-2.fna.fbcdn.net/v/t1.0-9/72577_563558293672959_39183751_n.jpg?_nc_cat=101&_nc_sid=dd9801&_nc_ohc=YySniLzpf_sAX-B8YFE&_nc_ht=scontent.ffjr1-2.fna&oh=12da3f0ae2404066684d742b4f785cfc&oe=5ED9FCDA",
"https://img.particlenews.com/image.php?type=thumbnail_1024x576&url=2zUI4r_0OJWDY4z00",
"https://content3.jdmagicbox.com/comp/def_content/advertising_agencies/default-advertising-agencies-9.jpg",
"https://miro.medium.com/max/895/1*2gq5_jgNSYJnLzCqIIYVGA.jpeg"]
}
Here below my registered cell that I want to make the autoscroll inside it by infinite array looping .
So can you help me how to do that?
extension HomeVC: UICollectionViewDelegate, UICollectionViewDataSource {
func setupCollectionViewCell () {
collectionView.delegate = self ; collectionView.dataSource = self
collectionView.register(UINib(nibName: "homeAdCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "adCell")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewlayout: UICollectionViewLayout, sizeForItemAt IndexPath: IndexPath) -> CGSize {
print ("Size func called")
return CGSize(width: 250, height: 75)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "adCell", for: indexPath) as! homeAdCollectionViewCell
cell.update(SURL: array[indexPath.row])
return cell
}
}
Set the Timer in ViewDidLoad and scroll to next item using collectionView.ScrollToitem method
If you want infinite scrolling with autoscroll effect, you can check this library:
https://github.com/WenchaoD/FSPagerView
Related
So I have an issue I've been battling with for days now. I have a collectionView which is designed to have large cells (width-wise) which go off screen. The height is fixed. The idea is to basically scroll to the end (see below):
To achieve being able to scroll left and right, I've had to override the width inside collectionViewContentSize in my flowLayout. Like below.
override var collectionViewContentSize: CGSize {
var size = super.collectionViewContentSize
size.width = largeWidth
return size
}
This achieves increasing the horizontal scroll area (which is what I want) but the cells start to disappear once I reach a certain point. It's almost as if the cells are being dequeued when they shouldn't be. Any ideas on this. This is the final straw for my project but I'm out of ideas.
Many thanks
Code snippet can be found below. You should be able to just copy and paste this into any project:
class HomeViewController: UIViewController {
let collectionView: UICollectionView
let collectionViewLayout = CustomCollectionViewFlowLayout()
init() {
collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout)
super.init(nibName: nil, bundle: nil)
collectionView.backgroundColor = UIColor.red.withAlphaComponent(0.4)
collectionView.register(SomeCell.self, forCellWithReuseIdentifier: "SomeCell")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.delegate = self
collectionView.dataSource = self
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.frame = view.bounds
view.addSubview(collectionView)
}
}
class SomeCell: UICollectionViewCell {
}
extension HomeViewController: UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 150
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SomeCell", for: indexPath) as! SomeCell
cell.backgroundColor = .blue
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 10000, height: 70)
}
}
class CustomCollectionViewFlowLayout: UICollectionViewFlowLayout {
override var collectionViewContentSize: CGSize {
var size = super.collectionViewContentSize
size.width = 10000
return size
}
}
I am struggled in passing images from collection view cell to another collection view cell.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "ImageViewController") as! ImageViewController
let imageItem = image[indexPath.row]
vc.images = imageItem.image
self.navigationController?.pushViewController(vc, animated: true)
}
then in ImageViewController
class ImageViewController: UIViewController ,UICollectionViewDelegate , UICollectionViewDataSource {
#IBOutlet weak var imageCollectionView: UICollectionView!
var image: string!
override func viewDidLoad() {
super.viewDidLoad()
self.imageCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return image.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! imageCollectionViewCell
let url: URL = NSURL(string: image)! as URL
cell.images.af_setImage(withURL: url)
return cell
}
import UIKit
//for the firstVC
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main" , bundle: nil)
if let vc = mainStoryboard.instantiateViewController(withIdentifier: "ImageViewController") as? ImageViewController
vc.reciveImageFromFirstVC = image[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
// for the second page image view controller
class ImageViewController : UIViewController {
var image:String!
var reciveImageFromFirstVC : String? {
didSet {
guard let reciveImg = reciveImageFromFirstVC else {
print("Can't get image for some reason")
return}
self.image = reciveImg
self.imageCollectionView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! imageCollectionViewCell
let url: URL = NSURL(string: image)! as URL
cell.images.af_setImage(withURL: url)
return cell
}
}
i hope i helped you
thanks
I would like to create list of rows using UICollectionView with each row can click and go to another controller.
I create custom cell in xib and class for it.
This is my code:
class CollectionCellXIB: UICollectionViewCell {
#IBOutlet weak var label: UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
class Collection: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var collectionView: UICollectionView!
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell: CollectionCellXIB
self.collectionView!.registerNib(UINib(nibName: "CollectionCellXIB", bundle: nil), forCellWithReuseIdentifier: "cell")
if indexPath.row == 0 {
cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionCellXIB
cell.label.text = "cell 1"
} else {
cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CollectionCellXIB
cell.label.text = "cell 2"
}
return cell
}
}
Unfortunatelly, segues not working. When I clicked on a row, nothing happend. Method shouldPerformSegueWithIdentifier wasn't called.
So, question is, how to use custom cells with xib and using segues?
In my custom view controller defined as follows:
class Wall : UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, WallLayoutDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let layout = WallLayout()
layout.delegate = self
let collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.registerClass(WallCell.self, forCellWithReuseIdentifier: Wall.reuseIdentifier)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.blackColor()
self.view.addSubview(collectionView)
self.collectionView = collectionView
}
// CODE
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(50, 100)
}
}
The function sizeForItemAtIndexPath is never called. How it comes?
I found that it is because the layout is up to me when I use UICollectionViewDelegateFlowLayout and cannot be deduced by the collection view.
I am trying to implement an alarm clock app just like apple's Clock app.On the click of Edit button on the left hand side I want to make table enter into Editing mode with red circles on left side of every cell(custom UITableViewCell) And on the click of that red circle want to show "Delete" button/action on the right side.
I have been trying a lot and went through many sites but still could not figure out. Can someone please see what mistake I am making?
I have referred below and many others links:
How to enable swipe to delete cell in a TableView?
UITableViewCell, show delete button on swipe
class SavedAlarmListViewController: UIViewController,UITableViewDataSource,UITableViewDelegate{
#IBOutlet weak var tableView: UITableView!
var alarms = [AlarmDataObject]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.navigationItem.leftBarButtonItem = self.editButtonItem()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(true)
refreshList()
}
func refreshList() {
alarms = AlarmsList.sharedInstance.allSavedAlarms()
tableView.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return alarms.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AlarmTableViewCell
// custom code to set data ....
return cell
}
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true // all cells are editable
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
let alarm = alarms.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
AlarmsList.sharedInstance.removeAnAlarm(alarm)
}
}
}
class AlarmTableViewCell:UITableViewCell {
// IBOutlets
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
You should implement
tableView(_:editingStyleForRowAtIndexPath:) and return .Delete