Poor Performance with multiple UICollectionViewCells in one CollectionVIew - ios

I have a CollectionView with multiple cell types. When scrolling between different cell types performance can get very laggy. I'm not exactly sure how to fix it. Here's my cellForItemAtIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if cellType == "imageCell" {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("iCell", forIndexPath: indexPath) as! ImageCell
//do things for cell
return cell
}else if cellType == "mapCell" {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mCell", forIndexPath: indexPath) as! MapCollectionViewCell
//do things for cell
return cell
}else if cellType == "pCell" {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("pCell", forIndexPath: indexPath) as! PCell
//do things for cell
return cell
}else if cellType == "detailCell" {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("fCell", forIndexPath: indexPath) as! DetailCell
//do things for cell
return cell
}else{
return UICollectionViewCell()
}}

Related

Value of type 'UICollectionViewCell?' has no member 'contentImage'

I have two custom UICollectionViewCells(AddImageCollectionViewCell, ItemCollectionViewCell) which I am loading depending on indexpath. Here is my code for the cellForItemAtIndexpath-
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell :UICollectionViewCell!
if indexPath.row == 0{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addImageCell", for: indexPath) as! AddImageCollectionViewCell
}
else{
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as! ItemCollectionViewCell
cell.contentImage = self.droppedItemList[indexPath.row] //error here
}
return cell
}
I am getting the error as "Value of type 'UICollectionViewCell?' has no member 'contentImage'". Why is my cell in the else clause is not cast to "ItemCollectionViewCell" type.
I know, I must be doing something very foolish. I would be really grateful if someone can point me to the right direction.
You are declaring cell as basic type UICollectionViewCell, that's the reason. Return the cells separately
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.row == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addImageCell", for: indexPath) as! AddImageCollectionViewCell
return cell
} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemCell", for: indexPath) as! ItemCollectionViewCell
cell.contentImage = self.droppedItemList[indexPath.row]
return cell
}
}

identify many cell in uitable view

I want to identify many custom cell in table view "i build them in storyboard " but the are error ask from me to return value , i am trying to return nil and int value and cell but the error be same
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("CheefsCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 1 {
let cell = tableView.dequeueReusableCellWithIdentifier("BeautyCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 2 {
let cell = tableView.dequeueReusableCellWithIdentifier("StudentServicesCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 3 {
let cell = tableView.dequeueReusableCellWithIdentifier("ArtAndDesigneCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 4 {
let cell = tableView.dequeueReusableCellWithIdentifier("StoreCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
else if indexPath == 5 {
let cell = tableView.dequeueReusableCellWithIdentifier("OthersCell", forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .DisclosureIndicator
return cell }
return
}
update ::
git hub link /
The method cellForRowAtIndexPath requires to return a non-optional UITableViewCell, so you must ensure to return a cell in any case. A simple return or return nil is not allowed.
The code can be simplified, most of it is redundant. The only difference is the identifier.
A suitable solution is a switch statement on the row property of the indexpath.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var identifier : String
switch indexPath.row {
case 0: identifier = "CheefsCell"
case 1: identifier = "BeautyCell"
case 2: identifier = "StudentServicesCell"
case 3: identifier = "ArtAndDesigneCell"
case 4: identifier = "StoreCell"
default: identifier = "OthersCell"
}
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
cell.accessoryType = .DisclosureIndicator
return cell
}
You're getting an error cause you're not returning a cell from the method.
Change your code so it always returns a cell.
When checking indexPaths, use their row property - indexPath.row.
Consider replacing your if-else tree with switch.
Another example based on #vadian's answer:
static let identifiers = [ "CheefsCell", "BeautyCell", "StudentServicesCell", "ArtAndDesigneCell", "StoreCell" ]
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = (indexPath.row < count) ? identifiers[ indexPath.row ] : "OthersCell"
var cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
if cell == nil {
cell = UITableViewCell() // allocate a new cell here
}
cell.accessoryType = .DisclosureIndicator
// configure your cell here
return cell
}

Using multiple UITableview in Single ViewController with Custom TableViewCell

How can I return a TableViewcell in cellForRowAtIndexPath delegate method , if I m using multiple UITableView in a Single ViewController, One TableView has Custom UITableViewCell and the other has default UITableViewCell . My problem is I m not able to cast the UITableViewCell to Custom TableViewCell type .
Code used as follows ,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell?
if tableView == self.tvCity {
var cell:UITableViewCell?
cell = tableView.dequeueReusableCellWithIdentifier(cityCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell!.textLabel?.text = self.cityList[row].cityEn
}
if tableView == self.tvBranchByCity {
var cell:BranchNearMeTableViewCell?
cell = (tableView.dequeueReusableCellWithIdentifier(branchCellIdentifier, forIndexPath: indexPath) as! BranchNearMeTableViewCell)
let row = indexPath.row
cell.branchName = self.branchList[row].name// here cell.branchName is not accessible.
}
return cell!
}
Please advise . Thanks in advance.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == self.tvCity {
var cell:UITableViewCell?
cell = tableView.dequeueReusableCellWithIdentifier(cityCellIdentifier, forIndexPath: indexPath) as UITableViewCell
let row = indexPath.row
cell!.textLabel?.text = self.cityList[row].cityEn
return cell!
}
if tableView == self.tvBranchByCity {
var cell:BranchNearMeTableViewCell?
cell = (tableView.dequeueReusableCellWithIdentifier(branchCellIdentifier, forIndexPath: indexPath) as! BranchNearMeTableViewCell)
let row = indexPath.row
cell.branchName = self.branchList[row].name as! String // here cell.branchName is not accessible.
return cell!
}
}
Differentiate tables with tag
see example :
if tableView.tag == 2000
{
identifier = "First Table"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
return cell
}
else
{
identifier = "Second Table"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
return cell
}

Trying to add a section every 2 indexPath.row swift

Having a little problem with tableviews. I want to add a section to the tableview without messing with the content of the other data in the table. I want to add (cell3, thiscell) after every 2 indexpath.row
What i have is a section on the storyboard which is one cell that i want to add after every 2 index path without messing with all the other information. How would i use the insert section function? or is there any other way to do it?
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
if indexPath.section == 0 && cell1 == true{
let cellIdentifier = “cell1"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
cell.nameLabel.text = “cell1"
cell.subNameLabel.text = ""
return cell
} else if indexPath.section == 2 && vally == true{
let cellIdentifier = "cell2"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
cell.nameLabel.text = “Smiles"
cell.subNameLabel.text = ""
return cell
} else if indexPath.row == 2 {
let cellIdentifier = “cell3"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! thisCell
print("add this after indexpath 2")
return cell
} else if indexPath.section == 1 {
let cellIdentifier = "SubCategoryTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
cell.loading.hidden = false
cell.loading.startAnimating()
cell.nameLabel.text = subCat[indexPath.row].subCategoryName
cell.subNameLabel.text = subCat[indexPath.row].subCategoryDesc
return cell
}
let cell2: SubCategoryTableViewCell = tableView.dequeueReusableCellWithIdentifier("SubCategoryTableViewCell", forIndexPath: indexPath) as! SubCategoryTableViewCell
cell2.userInteractionEnabled = false
return cell2
}
You want to display for cell3 for indexPath.row = 2 in all sections or any specific section?
In case if you want to display in all sections:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Table view cells are reused and should be dequeued using a cell identifier.
if(indexPath.row == 2){
let cellIdentifier = "cell3"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! thisCell
print("add this after indexpath 2")
return cell
}
else if indexPath.section == 0 && cell1 == true{
let cellIdentifier = "cell1"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
cell.nameLabel.text = “cell1"
cell.subNameLabel.text = ""
return cell
} else if indexPath.section == 2 && vally == true{
let cellIdentifier = "cell2"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
cell.nameLabel.text = “Smiles"
cell.subNameLabel.text = ""
return cell
} else if indexPath.section == 1 {
let cellIdentifier = "SubCategoryTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SubCategoryTableViewCell
cell.loading.hidden = false
cell.loading.startAnimating()
cell.nameLabel.text = subCat[indexPath.row].subCategoryName
cell.subNameLabel.text = subCat[indexPath.row].subCategoryDesc
return cell
}
let cell2: SubCategoryTableViewCell = tableView.dequeueReusableCellWithIdentifier("SubCategoryTableViewCell", forIndexPath: indexPath) as! SubCategoryTableViewCell
cell2.userInteractionEnabled = false
return cell2
}
And above code will add new cell cell3 in each section at indexPath.row -> 2
The indexPath.row value is intended to match indexes your underlying array of supporting data. Presenting additional "virtual" cells in place of rows does not add them, it merely shows a different cell at the corresponding row.
You have two choices:
1) take into account the progressive offset that your "in-between" cells create in the subCat[row] and the IndexPath.row.
2) use actual sections and segment and mat pairs of entries to indexPath.row 0 and 1 in each section.
For #1, you could create a pair of mapping functions and use them to convert indexPath.row to and from indexes in subCat[]
func indexFromRow(row:int) -> int?
{ return row % 3 == 2 ? nil : row - row/3 }
func rowFromIndex(index:Int) -> Int
{ return index + index/2 }
Then, when returning you cell, you can use the functions to establish the correspondance between rows and indexes:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
if let subCatIndex = indexFromRow(indexPath.row)
{
// build a SubCategoryTableViewCell cell, feed it with subCat[subCatIndex] data and return it
}
else
{
// build a thisCell cell and return it
}
}
you will also need to adjust the number of rows returned for the section to make room for the intermediate cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{ return rowForIndex(subCat.count-1) + 1 }
Everywhere in you code where you receive an IndexPath and need to know the index in subCat[], you'll need to use the rowToIndex() function
When you want to know the index path of an item in subCat[] at a given index, you'll have to us indexToRow() to build the indexPath
For #2, you will have to use a similar technique to map indexPath.row and indexes in subCat[], but you'll also have to deal with sections and I don't think you can use a reusable cell for section headers.

Sub-Class multiple Cells

In my UITableView I have Four cell. Each cell has its own class. But I can only return one of the cells. Here is my code
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let firstCell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forIndexPath: indexPath) as! FirstTableViewCell
let secondCell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! SecondTableViewCell
let thirdCelll = tableView.dequeueReusableCellWithIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdTableViewCell
let fourthCell = tableView.dequeueReusableCellWithIdentifier("FourthCell", forIndexPath: indexPath) as! FourthTableViewCell
return firstCell
}
How do I Correctly sub class and return them
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 // or your custom condition
{ let firstCell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forIndexPath: indexPath) as! FirstTableViewCell
return firstCell
}
else if indexPath.row == 1{
let secondCell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! SecondTableViewCell
return secondCell
}
indexPath.row == 2{
let thirdCelll = tableView.dequeueReusableCellWithIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdTableViewCell
return thirdCelll
}
else{
let fourthCell = tableView.dequeueReusableCellWithIdentifier("FourthCell", forIndexPath: indexPath) as! FourthTableViewCell
return fourthCell
}
}
This function is called multiple times. The indexPath.row would be used to determine which cell to return. You should check the value of this and only return the requested cell.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell
switch indexPath.row {
case 0:
cell = tableView.dequeueReusableCellWithIdentifier("FirstCell", forIndexPath: indexPath) as! FirstTableViewCell
case 1:
cell = tableView.dequeueReusableCellWithIdentifier("SecondCell", forIndexPath: indexPath) as! SecondTableViewCell
case 2:
cell = tableView.dequeueReusableCellWithIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdTableViewCell
case 3:
cell = tableView.dequeueReusableCellWithIdentifier("FourthCell", forIndexPath: indexPath) as! FourthTableViewCell
}
return cell;
}

Resources