I have a collection view with sections, when the user did click on a cell, I'm trying to mark the as selected or checked, and then react on the selected cells, but the error I have is:
each section has 12 cells, when I select the cell everything is good I can mark it as checked, but it marks also another cell, it mark cell every 5 cells, if I downgrade the cells per section to 5, everything is ok it marks only 1 cell.
this is my code:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("videoCell", forIndexPath: indexPath) as! VideoCell
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! VideoCell
if cell.checked == true {
cell.uncheck()
}
else {
cell.check()
}
}
Related
I have a double direction scrolling collectionView with 50 sections, 10 items per section and 3 showing cells per section and filling the collectionView with the same data array.
the problem is the collectionView duplicate data in every section and I want to show different data in appearing sections to the user.
Any help please!
this is my code:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 50
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: contentCellIdentifier,
for: indexPath) as! UserCollectionCell
cell.userImageView.kf.setImage(with: URL.init(string: String().loadCorrectUrl(url:firebaseUsers[indexPath.row].image)))
cell.userNameLabel.text=firebaseUsers[indexPath.row].name
if(selectedIndexPaths.contains(indexPath) ){
cell.userBackgroudView.isHidden=false
}
else{
cell.userBackgroudView.isHidden=true
}
}
return cell
}
Implement prepareForReuse in UserCollectionCell and clear the imageView and label text to prevent showing duplicate data on scroll
class UserCollectionCell: UICollectionViewCell {
override func prepareForReuse() {
super.prepareForReuse()
self.userImageView.image = nil
self.userNameLabel.text = nil
}
}
Hope it helps
I am practicing some coding on collectionViews. However I like to work in Main.storyboard to see what I am working with visually. Unfortunately it appears that makes more work on my end when working in the viewController because this would have been much faster if done programmatically.
I am trying to call for multiple reuse identifiers for the collectionViewCell. However I only know how to call one cell. Now I am not getting any errors, I just do not know how to display all of the cells when I run the program.
Here is the code.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Fashion Cell 0", for: indexPath)
return cell
}
When I run the app I get this.
Please note, in Main.Storyboard I am add the cells manually.
Example:
Do in this way
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Fashion Cell 0", for: indexPath)
return cell
}
if indexPath.item == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Fashion Cell 1”, for: indexPath)
return cell
}
//……
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Fashion Cell 0", for: indexPath)
return cell
}
Although you can use switch instead of if for better readability
You can not in this way.Create one array of your number of images and set it to collectionView. No need to more cell in storyboard.
let video:[UIImage] = ["","","",""]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return video.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)as! yourcellfile
let image = video[indexPath.row]
cell.img.image = image
return cell
}
You can go this way. It's proper method to imaplement collectionview.
In top of that image you can see some images and plus button I want to do like this in my app so i used collection view to get this view and everything works fine but i cant add cell to my dynamic collection view can some one help me to do this process
code in my view controller:
import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage
import SwiftElegantDropdownMenu
class EditPendingViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate{
var pendingImageString:[String]!
#IBOutlet var editPendingCollectionvIEW: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pendingImageString.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("editPendingCell", forIndexPath: indexPath) as!MypendindeditCollectionViewCell
let img:NSString = pendingImageString[indexPath.item]
cell.editImage.sd_setImageWithURL(NSURL(string: imageApikey + (img as String)))
cell.contentView.layer.borderColor = UIColor.lightGrayColor().CGColor
cell.contentView.layer.borderWidth = 1.0
return cell
}
}
I have not done any process for adding cell to my collection view i just loaded the data from the server.now help to add the cell
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pendingImageString.count + 1 // + 1 for last cell
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell
cell.btn.tag = indexPath.row
if indexPath.item = pendingImageString.count {
cell.imageview = //your static image that is shown in last cell
cell.cancelButton.isHidden = true
}
else{
let img:NSString = pendingImageString[indexPath.item]
cell.editImage.sd_setImageWithURL(NSURL(string: imageApikey + (img as String)))
cell.cancelButton.isHidden = false
}
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.item = pendingImageString.count{
// update your array with new image file
}
}
// cancel button action
func buttonClicked(sender: UIButton?) {
let tag = sender.tag
// remove object from array and reload collectionview
}
On click of last cell of the collection view add logic to enter your product in the database then just reload the collection view
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.row == pendingImageString.count - 1
{
//TODO - add item to database and reload collectionview
}
}
I want to put collection view inside tableview cell. Every tableview cell contains 2 collectionviewCell. Let say I have 8 pic in my data model, I want the collection view looks like the image. How to do this?
Instead Of Using Collection View inside tableview , Use only collection view with vertical scroll and in the method
#pragma mark ------------Collection View Delegates-----------------
override func numberOfSectionsInCollectionView(collectionView:
UICollectionView!) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView!,
numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(collectionView: UICollectionView!,
cellForItemAtIndexPath indexPath: NSIndexPath!) ->
UICollectionViewCell! {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,
forIndexPath: indexPath) as !MyCollectionViewCell
// Configure the cell
let image = UIImage(named: Images[indexPath.row])
cell.imageView.image = image
return cell
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(_collectionViewToAddImages.frame.size.width/2, _collectionViewToAddImages.frame.size.height/2);
}
In this way you will have view like this, just add the pading and you will get the results
Trying to get my head around collection views, arrays, and dictionaries. I have a class named CollectionViewCell that contains a UIImageView outlet that I want images from an array to populate. The trouble is that I have different sections with different content so I created multiple arrays that store the images. With the present code I get an error saying the array index is out of range. Do I need to populate the different sections with a dictionary that separates the information with keys and values instead?
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
cell.imageView.image = green[indexPath.row]
cell.imageView.image = blue[indexPath.row]
return cell
How can I name the different sections with headers that are populated from an array? names is my array of strings and HeaderView is a class containing an empty label. I also get an error using this code.
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let headerView =
collectionView.dequeueReusableSupplementaryViewOfKind(kind,
withReuseIdentifier: "HeaderView",
forIndexPath: indexPath)
as HeaderView
headerView.label.text = names[indexPath.row]
return headerView
default:
assert(false, "Unexpected element kind")
}
}
[edit]
The number of sections and number of items in section code:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//Return the number of sections
return 2
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Return the number of items in the section
return green.count
}
So if there's a different number of items in blue section as opposed to green, do I include return blue.count as well in the numberOfItemsInSection function?
Since green and blue are supposed to populate different sections, you need to use one or the other conditionally depending on the section, ex:
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Return the number of items in the section
if section == 0 {
return green.count
} else {
return blue.count
}
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell
// Configure the cell
if indexPath.section == 0 {
cell.imageView.image = green[indexPath.row]
} else {
cell.imageView.image = blue[indexPath.row]
}
return cell
}
In your current code, if blue is a shorter array than green, you can get the error you described, since the numberOfItemsInSection goes beyond blue's index.
The indexPath object encapsulates information not only about the row, but also about the section. So, if you are referring to the first element in the first section, the indexPath.row will be 0 and indexPath.section will be 0. If you are referring to the third element in the second section, indexPath.row will be 2 and indexPath.section will be 1 ... and so on. In this context, how would you define cellForRowAtIndexPathand numberOfItems methods? Here is the code:
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Return the number of items in the section
if(section == 0) {
return green.count
} else if(section == 1) {
return red.count
}
}
tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!) {
//Reusable cell code
if(indexPath.section == 0) {
//green cell population
}
else if(indexPath.section == 1) {
//red cell population
}
}