TableViewController with custom cells in swift - ios

I'm having an issue with custom TableViewCell in Swift. I'm trying to display a custome TableViewCell in a TableView but that doesn't work well. I got a grey square on the screen... I found some answers on the Internet that recommend to disable Auto-Layout. I did it but now, my cell is displayed but there is a mess with the height of the cells. As you can see, my cell that is supposed to have red background is displayed on the height of 3 cells while the background is on only 1 cell. The height of the cell and the Row Height are both set to 150px.
With Auto-Layout: Without Auto-Layout:
Here is the Cell code:
class CharacterDescription : UITableViewCell {
#IBOutlet var imageProfile: UIImageView!
#IBOutlet var name: UILabel!
#IBOutlet var hp: UILabel!
#IBOutlet var damages: UILabel!
#IBOutlet var range: UILabel!
#IBOutlet var mp: UILabel!
#IBOutlet var critChances: UILabel!
#IBOutlet var critModifier: UILabel!
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}
}
And the TableViewController code:
class CharacterDescriptionsViewController : UITableViewController, UITableViewDataSource, UITableViewDelegate {
var userProfile: UserProfile!
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init() {
super.init()
}
override func viewDidLoad() {
var nibName = UINib(nibName: "CharacterDescription", bundle:nil)
self.tableView.registerNib(nibName, forCellReuseIdentifier: "CharacterDescription")
}
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return userProfile.characters!.count
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
var cell = self.tableView.dequeueReusableCellWithIdentifier("CharacterDescription", forIndexPath: indexPath) as CharacterDescription
//!TODO: Init of cell
return cell
}
}

You have to override TableView.heightForRowAtIndexPath to return the height of your custom cell.

Related

How to set customView in UITableViewCell with Swift

I have UITableView, UITablaViewCell, CustomView
and UITableViewCell includes customView.
I'm trying to put Product's data to cell with my function not cellForRowAt.
Cell shows just origin view ProductView.xib with empty data
Please help.
ViewController.swift
struct Product {
let brand: String
let product: String
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! ProductTableViewCell
// this line is not work
// productView is not update
cell.productView = createProductView(product: product)
return cell
}
func createProductView(product: Product) -> ProductView {
let productView = ProductView()
productView.brandLabel.text = product.brand
productView.productLabel.text = product.product
return productView
}
UITableViewCell.swift
class ProductTableViewCell: UITableViewCell {
#IBOutlet var productView: ProductView!
}
ProductView.swift
class ProductView: UIView {
#IBOutlet weak var productView: UIView!
#IBOutlet weak var brandLabel: UILabel!
#IBOutlet weak var productLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit(){
let bundle = Bundle(for: ProductView.self)
bundle.loadNibNamed("ProductView", owner: self, options: nil)
addSubview(productView)
productView.frame = self.bounds
}
There are multiple issues with your code
Issue 1: cellForRowAt IndexPath gets called multiple times, with your code you will end up creating a new ProductView every time tableView is scrolled (cell is reused). instead you can create product view only once and update its label every time cell is reused
Issue 2: In ProductView's commonInit you set the frame using productView.frame = self.bounds self.bounds will always be(0,0,0,0). Because you have instantiated ProductView as ProductView()
Issue 3: createProductView is supposed to return an instance of ProductView hence the method signature is invalid so you should change it from func createProductView(product: Product) -> ProductView() to func createProductView(product: Product) -> ProductView as already suggested in answer above
What can be better solution?
class ProductTableViewCell: UITableViewCell {
var productView: ProductView!
func updateProductView(product: Product) {
productView.brandLabel.text = product.brand
productView.productLabel.text = product.product
}
override func prepareForReuse() {
super.prepareForReuse()
productView.brandLabel.text = nil
productView.productLabel.text = nil
}
override func awakeFromNib() {
super.awakeFromNib()
self.productView = createProductView()
self.addSubview(self.productView)
self.productView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.productView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
self.productView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
self.productView.topAnchor.constraint(equalTo: self.topAnchor),
self.productView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
}
func createProductView() -> ProductView {
return ProductView()
}
}
Finally in your cellForRowAtIndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! ProductTableViewCell
//assuming you already have a product for index path
cell.updateProductView(product: product)
return cell
}
EDIT:
As OP is facing issue with loading nib from bundle updating the answer here.
In your ProductView common init change the way you access bundle from
Bundle(for: ProductView.self) to Bundle.main as shown below
class ProductView: UIView {
#IBOutlet weak var productView: UIView!
#IBOutlet weak var brandLabel: UILabel!
#IBOutlet weak var productLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit(){
Bundle.main.loadNibNamed("ProductView", owner: self, options: nil)
addSubview(productView)
}
Few things to take care
Ensure you have an XIB named ProductView in your bundle
Ensure you have set its file owner to ProductView

How can I use a custom initializer on a UITableViewCell?

I have a custom UITableViewCell and I'd like to use it in my table view. Here's my code for the cell:
class ReflectionCell: UITableViewCell {
#IBOutlet weak var header: UILabel!
#IBOutlet weak var content: UILabel!
#IBOutlet weak var author: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
init(data: Reflection) {
self.header.text = data.title
self.content.text = data.content
self.author.text = data.author.name
super.init(style: UITableViewCellStyle.default, reuseIdentifier: "reflectionCell")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I have a model class Reflection that I'd like to initialize the cell with. However, in my view controller I need to use tableView.dequeueReusableCell(withIdentifier: "reflectionCell", for: indexPath). Is there any way for me to use a custom initializer like the one I made?
If you use dequeueReusableCell you cannot change which initializer method that is called. But you can write your own method to update the IBOutlets which you then call after you successfully dequeue a cell.
class ReflectionCell: UITableViewCell {
#IBOutlet weak var header: UILabel!
#IBOutlet weak var content: UILabel!
#IBOutlet weak var author: UILabel!
func update(for reflection: Reflection) {
header.text = reflection.title
content.text = reflection.content
author.text = reflection.author.name
}
}
And
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! ReflectionCell
cell.update(for: reflections[indexPath.row])
return cell
}

tableview not reading label

I am trying to update the cells on a table view but the cell is not reading the text I am passing and returning nil.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! MyCell
let item = self.items[indexPath.row]
myCell.nameLabel?.text = item as? String
print( self.items[indexPath.row])
myCell.myTableViewController = self
return myCell
}
class MyCell: UITableViewCell {
var myTableViewController: ChangeFeedViewController!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
#IBOutlet weak var nameLabel: UILabel! = {
let label = UILabel()
label.text = "Any Label"
return label
}()
#IBOutlet weak var actionButton: UIButton! = {
let button = UIButton()
button.setTitle("Action", for: .normal)
return button
}()
#IBAction func buttonHandleAction(_ sender: Any) {
handelAction()
}
func handelAction(){
myTableViewController.deleteCell(cell: self)
}
}
You need to modify your outlet like this and connect to your interface builder of MyCell class.
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var actionButton: UIButton!

didSelectRowAtIndexPath not getting called when row is pressed

I have 5 custom cells in a table view but didSelectRowAtIndexPath is not working for only one cell. I have added the cell for the row at the index for that cell in the code.
let cell : FileSentCell = chatTableView.dequeueReusableCell(withIdentifier: "fileSend") as! FileSentCell
let fileUrl = self.getImageURL(forFileName: fileNameString!)
if(fileUrl == nil){
cell.downloadStatusIndicator.image = #imageLiteral(resourceName: "fileDownloadWhite")
}else{
cell.downloadStatusIndicator.image = #imageLiteral(resourceName: "downloadedWhite")
}
cell.downloadStatusIndicator.isHidden = false
cell.downloadingIndicator.alpha = 0.0
cell.sendFileView.layer.cornerRadius = 10
cell.name.text = groupMsg.senderDisplayName
cell.message.text = groupMsg.message
cell.time.text = groupMsg.dateSent!
cell.fileStatus.text = groupMsg.status
cell.fileIcon.image = returnImage(fileName:groupMsg.message!)
cell.sendFileView.tag = indexPath.row
cell.sendFileView.addGestureRecognizer(longPressGesture)
cell.sendFileView.isUserInteractionEnabled = true
return cell
I have made UITableview have a single selection. and didSelectRowAtIndexPath is getting called for all the other reuse cells except for this one cell
Is there a reason or should I change some thing in the cellforat row
My FileSentCell class is
import UIKit
import QuartzCore
class FileSentCell: UITableViewCell {
#IBOutlet var sendFileView : UIView!
#IBOutlet var message : ChatLabel!
#IBOutlet var time : UILabel!
#IBOutlet var fileStatus : UILabel!
#IBOutlet var fileIcon : UIImageView!
#IBOutlet var downloadStatusIndicator : UIImageView!
#IBOutlet var downloadingIndicator: UIActivityIndicatorView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder) {
//fatalError("init(coder:) has not been implemented")
super.init(coder: aDecoder)!
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
One more thing is if I reduce the width of UIView inside content view and click the row where UIView is not being displayed didSelectRow is getting called, So didSelectRow is not getting called only when it is clicked on the UIView inside content View
Thanks in Advance.
Set:
cell.contentView.isUserInteractionEnabled = true

Swift: IBoutlets are nil in Custom Cell

I can't figure out why this custom cell is not being output.
I have a custom cell set up in a storyboard (no nib).
I have a text field and 2 labels which are nil when I try to access them in the custom cell class. I'm almost sure I have everything hooked up correctly, but still getting nil.
I've selected my table cell in the storyboard and set Custom Class to TimesheetTableViewCell
I've also control clicked on the table and set the datasource and delegate to be TimesheetViewController
My custom cell class:
import UIKit
class TimesheetTableViewCell: UITableViewCell {
#IBOutlet var duration: UITextField!
#IBOutlet var taskName: UILabel!
#IBOutlet var taskNotes: UILabel!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init?(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
println("Cell's initialised")// I see this
println(reuseIdentifier)// prints TimesheetCell
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setCell(duration: String, taskName: String, taskNotes: String){
println("setCell called")
self.duration?.text = duration
self.taskName?.text = taskName
self.taskNotes?.text = taskNotes
}
My controller:
class TimesheetViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet var timesheetTable: UITableView!
var items = ["Item 1", "Item2", "Item3", "Item4"]
override func viewDidLoad() {
super.viewDidLoad()
self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("TimesheetCell", forIndexPath: indexPath) as TimesheetTableViewCell
println(items[indexPath.row]) // prints corresponding item
println(cell.duration?.text) // prints nil
cell.setCell(items[indexPath.row], taskName: items[indexPath.row], taskNotes: items[indexPath.row])
return cell
}
The problem is this line:
self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")
Delete it. That line says: "Do not get the cell from the storyboard." But you do want to get the cell from the storyboard.
(Make sure, however, that the cell's identifier is "TimesheetCell" in the storyboard, or you'll crash.)
Pretty sure that you need to remove the line
self.timesheetTable.registerClass(TimesheetTableViewCell.self, forCellReuseIdentifier: "TimesheetCell")
from viewDidLoad().

Resources