I want my tableView to have subtitle as well as being able to dequeue properly. I have referred to this link but it does not work for my code. What should I do?
My code is currently like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
//Calling tableview for a reusable cell here will always return a cell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = developerArray[indexPath.row].developerName
cell.detailTextLabel?.text = developerArray[indexPath.row].developerHP
return cell
}
Swift 5
//Declare the variable cell Identifier
let reuseCellIdentifier = “cellIdentifier”;
//Implementation of cellForRowAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: reuseCellIdentifier)
if (!(cell != nil)) {
cell = UITableViewCell(style: .subtitle, reuseIdentifier: reuseCellIdentifier)
}
cell?.textLabel?.text = //Title text
cell?.detailTextLabel?.text = //Subtitle text
return cell!
}
You can create custom cell using nib by adding labels
For creating custom cell refer this link:
Custom UITableViewCell from nib in Swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.title.text = yourTitleArray[indexPath.row]
cell.detailLbl.text = yourDetailArray[indexPath.row]
return cell
}
So first of all, did you create your table using storyboard or code?
Either way you need to make sure you set the datasource and delegate to self, provided the class they are in conforms to :
UITableViewDataSource
and
UITableViewDelegate
myTable?.delegate = self
myTable?.dataSource = self
Also make sure you register your cell
myTable?.register(myCell.self, forCellReuseIdentifier: "myCell")
And when declaring your cell, you need to force it as the type
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myCell
Below is a working sample of tableview with a created cell. I hope this helps.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// Create the tableview object
var myTable:UITableView?
override func viewDidLoad() {
// Set the size and location of the tableview
myTable = UITableView(frame: CGRect(x: 0, y: 0, width: 300, height: 600))
// register your cell
myTable?.register(myCell.self, forCellReuseIdentifier: "myCell")
// set the background color of the table, note this wont make a difference unless the cell background is changed.
myTable?.backgroundColor = UIColor.clear
// set the datasource and delegate to self
myTable?.delegate = self
myTable?.dataSource = self
// This is jsut for style, wether there should be seperators or not, and if the user can select multiple lines
myTable?.separatorStyle = .none
myTable?.allowsMultipleSelection = true
// Add the table to your view
self.view.addSubview(myTable!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// This is declaring how many rows you want in your table. I have 1 but you can do it according to the size of your array.
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create the cell
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myCell
// set the title text for this cell
cell.title.text = "HelloWorld"
// return the cell
return cell
}
}
and this is the class for the cell we referenced above.
class myCell: UITableViewCell {
var title = UILabel()
var detail = UILabel()
override func awakeFromNib() {
super.awakeFromNib()
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
title.font = UIFont.boldSystemFont(ofSize: 16)
title.textAlignment = .center
self.contentView.addSubview(title)
self.contentView.addSubview(detail)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
title.frame = CGRect(x: self.contentView.frame.width / 2 - 200, y: 6, width: 400, height: 20)
detail.frame = CGRect(x: 150, y: 10, width: 280, height: 20)
}
}
Let me know if this helps. if you are doing it from story board let me know and I'll adjust.
Related
I am relatively new to UIKit. Currently, I am trying to create a UISwitch that will show up on a specific UITableView cell. However, I can't seem to figure out how to do this. Instead, I am getting a UISwitch on every single cell in the UITableView.
My code is below:
import UIKit
class SettingsVC: UIViewController {
var tableView = UITableView(frame: .zero, style: .insetGrouped)
let cells = ["Change Accent Color", "Change Currency Symbol", "Vibrations"]
let cellReuseIdentifier = "cell"
override func viewDidLoad() {
super.viewDidLoad()
createTableView()
setTableViewDelegates()
}
func createTableView() {
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
tableView.leftAnchor.constraint(equalTo: view.leftAnchor),
tableView.rightAnchor.constraint(equalTo: view.rightAnchor),
tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
func setTableViewDelegates() {
tableView.delegate = self
tableView.dataSource = self
}
}
extension SettingsVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell()
}
cell.textLabel?.text = cells[indexPath.row]
let switchView = UISwitch(frame: .zero)
switchView.setOn(false, animated: true)
cell.accessoryView = switchView
return cell
}
}
This is how my UITableView looks currently in the simulator.
This is how I would like the UITableView to look.
How would I be able to achieve the look I'm going for? Any help would be greatly appreciated.
The method tableView(_:cellForRowAt:) is used to create all cells for a table, so the code inside this method is called for each cell. You need to figure out a condition that distinguishes the cell with a UISwitch and run the corresponding piece conditionally. Conceptually, something like this:
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell()
}
cell.textLabel?.text = cells[indexPath.row]
if isSwitchNeeded { // Here.
let switchView = UISwitch(frame: .zero)
switchView.setOn(false, animated: true)
cell.accessoryView = switchView
}
return cell
}
There are some architectural options that might allow you do that. One of them is to rely on the index path. For instance, this should work in your raw example:
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell()
}
cell.textLabel?.text = cells[indexPath.row]
if indexPath.row == 2 {
let switchView = UISwitch(frame: .zero)
switchView.setOn(false, animated: true)
cell.accessoryView = switchView
}
return cell
}
And a million other ways.
First of all most likely you want to save the value of the switch, so create a property on the top level of the view controller
var enableVibrations = false
Second of all cells are reused. Even if there are only three cells it's good practice to set all UI elements to a defined state, that means to set the accessory view to nil if there is no switch.
And there is a dequeueReusableCell API which returns a non-optional cell.
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let title = cells[indexPath.row]
cell.textLabel?.text = title
if title == "Vibrations" {
let switchView = UISwitch(frame: .zero)
switchView.setOn(enableVibrations, animated: true)
switchView.addTarget(self, action: #selector(toggleVibrations), for: .valueChanged)
cell.accessoryView = switchView
} else {
cell.accessoryView = nil
}
return cell
}
And add the action method
#objc func toggleVibrations(_ sender : UISwitch) {
self.enableVibrations = sender.isOn
}
I am trying to make a tableViewController with a custom cell class programmatically but I am getting this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier cellId - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
I have several UItableViews in my app with custom cells and they all work fine but for some reason, this one isn't working. I've spent the last few hours trying to work it out but can't see the problem, can someone help me please?
This is my tableview class:
var tableView = UITableView()
var twoDimensionalArray = [
cameraInfoStruct(cameraInfo: ["Camera Body","Lens", "FPS", "Shutter Angle", "Colour Temperature", "Resolution Width", "Resolution Height", "Format", "Camera Rig"]),
cameraInfoStruct(cameraInfo:["Focus Length", "F - Stop", "Camera Height", "Camera Tilt", "Camera Roll", "Filter"])
]
let cellId = "cellId"
let screenHeight = UIScreen.main.bounds.height
let screenWidth = UIScreen.main.bounds.width
let sectionHeaders = ["General", "Advanced"]
override func viewDidLoad() {
super.viewDidLoad()
addTableView()
}
func addTableView() {
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight))
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
tableView.dataSource = self
tableView.delegate = self
tableView.tableFooterView = UIView()
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sectionHeaderArray = sectionHeaders as? [String] {
return sectionHeaderArray[section]
}
return "unknown"
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return twoDimensionalArray[section].cameraInfo.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionHeaders.count
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.backgroundColor = UIColor.white
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! cameraInfoCell
let shotNumber = twoDimensionalArray[indexPath.section].cameraInfo[indexPath.row]
cell.textLabel?.text = shotNumber
return cell
}
}
And this is my custom cell class:
class cameraInfoCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
self.textLabel?.frame.origin.x = 20
self.detailTextLabel?.frame.origin.x = 20
self.detailTextLabel?.textColor = UIColor(red:0.73, green:0.73, blue:0.73, alpha:1.0)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
You use two different cellIDs:
In your class, you define a cellId variable,
let cellId = "cellId"
which you use in cellForRowAt:
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! cameraInfoCell
In addTableView, you use the hardcoded string "MyCell":
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
To fix it, use the cellId in both places, so change the code in your addTableView method to:
tableView.register(cameraInfoCell.self, forCellReuseIdentifier: cellId)
You have to register the table inviewDidLoad either
tableView.register(cellClassName.self, forCellReuseIdentifier: "cellId")
I'm trying to create a tableView that has (for now) only a button in each cell.
I hard coded the number of rows to be 100:
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
And this is my cellForRowAt function that creates the button and is suppose to properly place it in each cell:
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
let row = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let btn = UIButton(type:(UIButtonType.custom)) as UIButton
btn.backgroundColor = UIColor.black
btn.setTitle("(read more)", for: UIControlState.normal)
btn.titleLabel!.font = UIFont(name: "Helvetica", size: 10)
btn.frame = CGRect(x:300, y:row.bounds.height, width:70, height:20)
btn.addTarget(self, action: Selector(("buttonPressed:")), for: UIControlEvents.touchUpInside)
btn.tag = indexPath.row
cell.contentView.addSubview(btn)
return cell
}
So this places the buttons in each cell, but when I click on the cell, the associated button disappears or when i scroll down, most of the first few buttons are gone when I scroll back up. What am I doing wrong here? Any help would be greatly appreciated!
Thanks in advance.
This example maybe will help you:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet fileprivate weak var tableView: UITableView!
fileprivate let numberOfItems = 100
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset.top = 44
}
#objc fileprivate func buttonPressed(sender: UIButton) {
print("tag: \(sender.tag)")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfItems
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let accessory = UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 20))
accessory.backgroundColor = .black
accessory.setTitle("Read more", for: .normal)
accessory.titleLabel?.font = UIFont(name: "Helvetica", size: 12)
accessory.tag = indexPath.row
accessory.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
cell.accessoryView = accessory
return cell
}
}
you must create a cell everytime in cellForRowAt rather get the cell from dequeue and draw your content again. Creating a cell again and again is not an good idea. Finding own mistake and solving will another curve of learning.
l
et cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "Cell") // should not do
let row = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) // can use it.
both line together should not use.
I am creating a table view programmatically using custom cell.However when I test using VoiceOver Im getting "empty list" and "content is empty" even though there is value in the cell.Any suggestions?
My tableview -
func createTable(_ choicesArray:[String]){
myTableView = UITableView(frame: CGRect(x: 10, y: self.verticalPostion, width: 340, height: 300))
self.choicesData = choicesArray
myTableView.estimatedRowHeight = 70
myTableView.rowHeight = UITableViewAutomaticDimension
myTableView.allowsMultipleSelection = true
myTableView.register(AnswerCell.self, forCellReuseIdentifier: "cell")
myTableView.dataSource = self
myTableView.delegate = self
myTableView.isAccessibilityElement = false
myTableView.shouldGroupAccessibilityChildren = true
// myTableView.reloadData()
self.view.addSubview(myTableView)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
//more code for cell selection
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return choicesData.count
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, tableView);
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AnswerCell
cell.choiceLabel.text = self.choicesData[indexPath.row]
return cell
}
Code for Custom Cell -
class AnswerCell: UITableViewCell {
var choiceLabel = UILabel()
func configure(_ description: String) {
choiceLabel.text = description
applyAccessibility()
}
final func applyAccessibility() {
isAccessibilityElement = true
accessibilityLabel = choiceLabel.text!
accessibilityHint = "Double tap to play."
choiceLabel.isAccessibilityElement = false;
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(choiceLabel)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func layoutSubviews() {
super.layoutSubviews()
choiceLabel.frame = CGRect(x: 20, y: 0, width: 70, height: 30)
}
}
I know that adjusting UILabel height and UITableViewCell is probably a pretty standard issue, but I'm finding lots of answers that are based on the storyboard and inspectors, but not by just using Swift 3. I've created a table which covers the screen. The height for the cell is being determined as follows:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
My tableViewCell has a couple of objects in it, a UIImageView (myImage) and a UILabel (myText), set-up in a custom class. The positioning and sizing takes place in cellForRowAt.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomCell
cell.myImage.image = UIImage(named: "MyImage")
cell.myImage.layer.frame = CGRect(origin: CGPoint(x: 0, y: 10), size: (cell.myImage?.image?.size)!)
cell.myText.text = myArray[indexPath.row]
cell.myText.frame = CGRect(x: UIScreen.main.bounds.size.width * 0.25, y: 0, width: UIScreen.main.bounds.size.width * 0.7, height: 90)
cell.myText.numberOfLines = 0
return cell
}
The result is a bunch of stacked cells, overlapping each other. What should I do to adjust the height of the UILabel frame to the amount of text coming from myArray and adjust the cell height so that it's at least the height of myImage or myText?
You can make multiline label inside table view in Swift 3.
import UIKit
private let useAutosizingCells = true
class TableViewController: UITableViewController {
fileprivate let cellIdentifier = "Cell"
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
//setup initial row height here
if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
}
// MARK: - UITableViewDataSource
extension TableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return detailsModel.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
let details = detailsModel[indexPath.row]
cell.textLabel?.text = details.title
cell.detailTextLabel?.text = details.description
if useAutosizingCells && tableView.responds(to: #selector(getter: UIView.layoutMargins)) {
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
}
return cell
}
}