I created a table view and cell is custom style, I did hook up the cell with custom class and cell's id in dequeueReusableCell(withIdentifier: , for:)function.
I added an image view in the cell, and add it as IBOutlet into custom cell class. In custom cell class, I created get and set variable called tableImage to change the image in the image view.
But in
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
I still can't set tableImage. The error shows Value of type UITableViewCell has no member tableImage.
Have you add as! yourCustomTableViewCell after the dequeueReusableCell function?
yourTableView.dequeueReusableCell(withIdentifier: "yourCell", for: indexPath) as! yourCustomTableViewCell
Related
In this screen how to show (blue view) in between tableview rows
design image
code: in storyboard design i have given all static data in labels and images so with this below code i am getting all cells like above screen shot, but after three cells how to show blue box view, please suggest me
import UIKit
class ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
return cell
}
There are two ways to do that:
Use a different UITableViewCell class (probably what you are looking for?)
Use sections
How?
You can either create a new UITableViewCell prototype cell in your storyboard or do it programmatically.
Create a custom UITableViewCell as such:
class OfferTableViewCell: UITableViewCell {
}
// If you are not using storyboards, add the following code
// in your viewDidLoad
tableView.register(OfferTableViewCell.self, forCellReuseIdentifier: "your_cell_id")
Then, you can dequeue the newly created cell at any index as such:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 10 // Index of the different cell
let cell = tableView.dequeueReusableCell(withIdentifier: "your_cell_id", for: indexPath) as! OfferTableViewCell
// Do cell configuration here
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "BidCell", for: indexPath)
return cell
}
}
Keep in mind that this cell is going to take the place of another cell if you are using an array as datasource, so using myArray.count for your numberOfRowsInSection would result in the last array element missing. You will have to take that into account.
Resources
Using multiple custom cells in a TableView
UITableView sections
Custom header for TableView sections
Is there a possibility to set a dynamic backgroundColor with static cells in a UITableView?
For example the user pressed a button in a UITableView and then all cells in the same view change to a specific color.
I already figured out how to set a backgroundColor when the view shows up:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.black
}
Now I want to achieve the same when the view has already loaded. Any suggestions?
You can access the currently visible cells (the ones that have passed over the willDisplay milestone) via the visibleCells property. You can iterate over this array and do whatever you need:
for cell in tableView.visibleCells {
cell.backgroundColor = UIColor.black
}
Alternatively you can call reloadData() on the tableView, this will result in all cells being re-created and re-displayed.
You can use a delegate method called DidSelect this method works when a user tap on a cell in the tableView or inside an element, and returns the index path of selected Cell you can use that to edit background color or anything.
The code should be something like this optional func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath) inside this function you can get the cell using cellForRowAtIndexPath and pass it the selected path and cast it as the cell type you have and then edit it or do whatever.
The full code would look something like this
optional func tableView(_ tableView: UITableView,
didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
//use the cell object and do what you want
}
I have a collection view which will have to load the content of the cells but I don't want to use images from assets. So, I create a class with Paint Code which draws some images for me. However, I don't know how to load the images that paint code drew for me in my UICollectionView cells using dequeueReusableCell.
I'm gonna usa a TableView as example, but it's the same thing with a UICollectionView...
Create a subclass of UICollectionViewCell.
In this example I'm calling it ItemCell:
class ItemCell: UITableViewCell {...}
Create and configure a .xib file.
This file lays out your cell, and holds a UIView that represents your PaintCode drawable:
In Storyboard, set the class of your UICollectionViewCells as your custom subclass (in this example ItemCell).
The key thing here is to give it an identifier, so it can be dequeued:
Code example to reuse your custom cell:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = UITableViewCell()
// Constants.itemCellName matches the above identifier.
if let itemCell = tableView.dequeueReusableCell(withIdentifier: Constants.itemCellName) as? ItemCell {
configure(itemCell, at: indexPath)
cell = itemCell
}
return cell
}
Profit:
I have a table view and I would like the user to select only one item from the table view, then I want to add a checkmark to the cell. At the same time, I also want the checkmark on other cells to disappear so the. user can only select one at a time. Then I want to save the data (preferably using UserDefaults). How do I do that?
Create a class variable of type IndexPath. In your cellForRowAt method set .checkmark accessory type only if index path matches the class variable and .none otherwise. In didSelectRowAt method update the indexPath of selected variable and just reload the tableview
you should Use in this way
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let sgp : UITableViewCell = tableView.cellForRow(at: indexPath)!
sgp.accessoryType = .checkmark
}
I have a UITableViewCell that contains a custom UIView that I created (and some other stuff). My custom view extends UIView, and overrides drawRect for some custom drawing.
My tableview renders correctly when each cell is loaded initially, but when I scroll through the table cells are being re-used, and the custom UIView.drawRect method is not being re-called for them. This is resulting in a stale UIView being used for cells in the tableview.
Am I doing something wrong? I tried setting setNeedsDisplay on the tableview cell, but that did not work.
It turns out I was calling setNeedsDisplay on the UITableViewCell. When I call it on the custom UIView inside of the table view cell it works.
I was calling it within the cellForRowAt and it managed to update the charts on the custom Tableview UIView I put inside.
Code per below:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this is cast AS! ErgWorkoutCell since this is a custom tableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "ergWorkoutCell") as? ErgWorkoutCell
cell?.ergWorkoutTitle.text = jsonErgWorkouts[indexPath.row].title
cell?.ergWorkoutTime.text = String(FormatDisplay.time(Int(jsonErgWorkouts[indexPath.row].durationMin * 60)))
cell?.ergLineChartView.setNeedsDisplay()
return cell!
}