UITableView with 3 custom cells but 1 different - xcode

I have a TableView and I have created custom cells for that TableView with images, labels..etc, But I want one cell to be different than the others with It's own content ( cells will get their data from a backend).
So how to create 2 different types of cells in the same TableView, and how to arrange them, for example put 1 type of cell between 2 others ?
Edit: My UITableView Class:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell
let otherCell: OtherCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! OtherCellTableViewCell
// Configure the cell...
return myCell
}

If you're using a storyboard, just drag another UITableViewCell into the table view to be used as a prototype. Give it a unique and subclass (if necessary). In -tableView:cellForRowAtIndexPath: dequeue either the cell you're currently dequeueing, or this new one.
If you're not using a storyboard you'll need to either
-registerClass:forHeaderFooterViewReuseIdentifier: or
-registerNib:forHeaderFooterViewReuseIdentifier: to make your other cell style available to the table view.
Here's a modified version of your code that should work:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if condition {
let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell
// Configure the cell...
return myCell
}
else {
let otherCell: OtherCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! OtherCellTableViewCell
// Configure the cell...
return otherCell
}
}

Related

UITableViewVibrantCell on UITableView

I would like to know if it was possible to use UITableViewVibrantCell on a UITableView (not controller). I can't figure out how to do it.
I'm doing :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView(tableView, cellForRowAt: indexPath) as! UITableViewVibrantCell
cell.blurEffectStyle = SideMenuManager.default.menuBlurEffectStyle
return cell
}
My UIViewController inherit from UITableViewDataSource and UITableViewDelegate.
Thank you for your help :)

Dynamic Type does not work with tableView.dequeueReusableCell(withIdentifier:)

I'm experiencing a quite strange behaviour for my UITableViewCells with style .subtitle regarding Dynamic Type. When using
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DesignElementCell", for: indexPath) //Dynamic Type does not work properly in this case
cell.textLabel?.text = "Test"
cell.detailTextLabel?.text = "Test"
return cell
}
the output looks like
As you can see the header text is scaled fine, while cell labels are not. When using
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "DesignElementCell")
cell.textLabel?.text = "Test"
cell.detailTextLabel?.text = "Test"
return cell
}
Everything works fine:
The strangest thing in the first case is, that when I change dynamic type size in settings and head back to my app, suddenly everything seems to work as long as I'm not popping my ViewController. Does anyone experienced something similar? Have a nice day :)
First, check if you registered cell, and set delegate&dataSource of tableView in ViewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(Cell.self, forCellReuseIdentifier:"CellIdentifier")
Then, define rowHeight & estimatedRowHeight of tableView in ViewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 150

Is it possible to create one tableViewCell that can be used in multiple Table Controllers?

I have about 3 or 4 table controllers that will all use the same tableview cell. I keep going back on fourth of the possible logic. Can I use the same tableViewCell in multiple tableView Controllers assuming the information is between the two is the same? Or will I have to create a new cell for each controller?
Yes you can.
I am assuming that you are using Swift.
Goto File -> New and select cocoaTouch class as follows.
Now name Your class for custom cell and make it subClass of UITableViewCell. also check the box which says "Also create Xib file"
Now design your cell in this Xib and create outlets in its .Swift file. Lets say you have a custom tableView cell which looks something like this
Which contains a label or ImageView or anyThing that you have in your cell. Now in your swift file of custom cell you can write a method like so
class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)
let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell
return cell
}
Now you can use this cell in any tableView in your application just like below
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.backgroundColor = UIColor.clearColor()
// do something with your cell
}
I hope it helps.
Update for Swift 3 and Swift 4:
class func cellForTableView(tableView: UITableView, atIndexPath indexPath: IndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.register(UINib(nibName: "YourCustomTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)
let cell = tableView.dequeueReusableCell(withIdentifier: kYourCustomTableViewCellIdentifier, for: indexPath) as! YourCustomTableViewCell
return cell
}
Yes you can use the table view cell in multiple tableView Controllers .

Swift 1.2 - List type undeclared

In my TableViewController i have this line of code
override func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
In this block i try to get a list of objects
let list = frc.objectAtIndexPath(indexPath) as! List
But i get this warning:
Use of undeclared type "List"
Anybody knows how to deal with it?
This is the full code:
var frc: NSFetchedResultsController = NSFetchedResultsController()
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",
forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
let list = frc.objectAtIndexPath(indexPath) as! list
cell.textLabel?.text = list.name
cell.detailTextLabel?.text = list.url
return cell
}
Any help is welcome!!
Greetings and Thanks
In the tutorial, Jason creates an entity called "List" in his Data Model file and then creates a class called List.
The following link starts the video just after he creates this file.
https://youtu.be/GeM7Zw12wbM?t=5m30s
You may have named your entity differently.
I named mine Record, since I'm storing records.
So my code will look like the following:
var frc: NSFetchedResultsController = NSFetchedResultsController()
override func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",
forIndexPath: indexPath) as! UITableViewCell
// Configure the cell...
// ALL occurrences of "list" are instead "record"
// ALL occurrences of "List" are instead "Record"
let record = frc.objectAtIndexPath(indexPath) as! Record
cell.textLabel?.text = record.name
cell.detailTextLabel?.text = record.url
return cell
}

Registering a Nib

I would like to set the values of detail labels in the View Controller file using an array with index path.row .
In a previous post:
[link][1]
someone helpfully suggested I use
var nib = UINib(nibName: "YourCellSubclass", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
however I'm new to using this and came across an error:
Cannot invoke registerNib with an argument list of type 'UINib, forCellReuseIdentifier: 'String'
My best guess would be, instead of this:
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
Should be:
var cell: YourSubClass = self.tableView.dequeueReusableCellWithIdentifier("cell") as YourSubClass
This compiles in Xcode 7:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MyCellSubclass
return cell
}
If you have multiple kinds of cells, you can go with:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let aCell = table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if let myCell = aCell as? MyCellSubclass {
return myCell
}
return aCell
}

Resources