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!
Related
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
}
I'm working on a swift assignment about table view controller and my table view doesn't have anything on it. I have correctly connected the labels and classes and assigned tableview cell identifier as "cell" in the attribute on storyboard but I still have the error "need to register nib" and even if I wrote self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell") the error is gone but there's still nothing on it.
My cell class is as below.
class cartTableViewCell: UITableViewCell {
#IBOutlet weak var itemImage: UIImageView!
#IBOutlet weak var itemName: UILabel!
#IBOutlet weak var itemQuan: UILabel!
#IBOutlet weak var itemFrame: UILabel!
#IBOutlet weak var itemSize: UILabel!
#IBOutlet weak var itemPrice: UILabel!
#IBOutlet weak var itemSub: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}`
my function is
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath) as! cartTableViewCell
let item = itemArray[indexPath.row]
cell.itemName?.text = item.itemName!
cell.itemFrame?.text = "haha"//itemArray[indexPath.row].itemFrame!
cell.itemSize?.text = item.itemSize!
cell.itemPrice?.text = item.sellingPrice!
//cell.itemQuan?.text =
cell.itemSub?.text = "\(Int(item.sellingNumber!)! * Int(item.sellingPrice!)!)"
return cell
}`
If I print(item.itemName), I can get some value. cell.itemName?.text is nil
I checked some answers online but so far no luck. Any help is highly appreciated.
Remove code
self.tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
Write below code in cartTableViewCell
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// code common to all your cells goes here
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
Now modify your code with below code
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath) as! cartTableViewCell
if cell == nil {
cell = cartTableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
let item = itemArray[indexPath.row]
cell.itemName?.text = item.itemName!
cell.itemFrame?.text = "haha"//itemArray[indexPath.row].itemFrame!
cell.itemSize?.text = item.itemSize!
cell.itemPrice?.text = item.sellingPrice!
//cell.itemQuan?.text =
cell.itemSub?.text = "\(Int(item.sellingNumber!)! * Int(item.sellingPrice!)!)"
return cell
}
Just check the reuse identifier name in your code. I am just writing this for more explanation.
viewDidLoad(){
let customNib = UINib(nibName: "CustomCell", bundle: Bundle.main)
tableView.register(customNib, forCellReuseIdentifier: "customCell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomCell
cell.configureCell(text: data[indexPath.row])
}
Never do this :
let cell = self.tableView.dequeueReusableCell(withIdentifier:"Cell", for: indexPath) as! cartTableViewCell
if cell == nil {
cell = cartTableViewCell.init(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
Explantion :
A UITableViewCell object with the associated reuse identifier. This
method always returns a valid cell. Ref:https://developer.apple.com/documentation/uikit/uitableview/1614878-dequeuereusablecell.
Do this, I just got this error today and I got it fixed by :
let yourNib = UINib(nibName: "yourNibName", bundle: Bundle.main)
yourTableView.register(yourNib, forCellReuseIdentifier: "cell")
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
I can t seem to get the prototypereuseidentifier thing on firebaseUI. If I add a prototypecell, I can only give it a cellidentifier
In my following code, my IBoutlets linked to my custom cell return nil.
here s my code :
func loadData() {
self.dataSource = FirebaseTableViewDataSource(ref: refpost, cellClass: MainWitnessTableViewCell.self, cellReuseIdentifier: "<RIcell>", view: self.tableView)
self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
let snap = obj as! FDataSnapshot
print(cell)
let mycell = cell as! MainWitnessTableViewCell
let keyy: String = (snap.value.objectForKey("author") as? String)!
mycell.postContent.text = keyy
print (mycell.postContent.text)
}
self.tableView.dataSource = self.dataSource
}
here, mycell.postContent.text returns nil, is there any sorcery that keeps blinding me ? :)
sincerely
Yann
I think you should change
cellReuseIdentifier: "<RIcell>"
to
cellReuseIdentifier: "RIcell"
# jonas : here s the cell class
class MainWitnessTableViewCell: UITableViewCell, UITextFieldDelegate {
#IBOutlet weak var cellImage: UIImageView!
#IBOutlet weak var postOwner: UILabel!
#IBOutlet weak var postDate: UILabel!
#IBOutlet weak var postContent: UITextView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
postContent.text = ""
postOwner.text = ""
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I'm trying to implement a counter as a UILabel using + and - UIButtons in a custom cell. I've managed to reference the row using addTarget and tags for the buttons, however I'm not sure how to update the label, adding or subtracting 1 each time a button is pressed.
I've tried accessing the cell.drinkQtyLabel.text directly from the addDrink function but that clearly doesn't work. My research pointed to using delegates, but I'm not proficient enough with Xcode to know what to do with that info.
Here is a screen shot of the cells:
Here is the custom cell.swift:
import UIKit
class ProductListTableViewCell: UITableViewCell {
#IBOutlet var drinkNameLabel: UILabel! = UILabel()
#IBOutlet var drinkVesselLabel: UILabel! = UILabel()
#IBOutlet var drinkSizeLabel: UILabel! = UILabel()
#IBOutlet var drinkPriceLabel: UILabel! = UILabel()
#IBOutlet var drinkQtyLabel: UILabel! = UILabel()
#IBOutlet weak var subtractDrinkButton: UIButton! = UIButton()
#IBOutlet weak var addDrinkButton: UIButton! = UIButton()
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
and the cellForRowAtIndex method:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:ProductListTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as ProductListTableViewCell
let drink:PFObject = self.productListData.objectAtIndex(indexPath.row) as PFObject
var Qty = 1
cell.drinkNameLabel.alpha = 0
cell.drinkVesselLabel.alpha = 0
cell.drinkSizeLabel.alpha = 0
cell.drinkPriceLabel.alpha = 0
cell.drinkQtyLabel.alpha = 0
cell.drinkNameLabel.text = drink.objectForKey("name") as? NSString
cell.drinkVesselLabel.text = drink.objectForKey("vessel") as? NSString
cell.drinkSizeLabel.text = drink.objectForKey("size") as? NSString
cell.drinkPriceLabel.text = drink.objectForKey("price") as? NSString
cell.drinkQtyLabel.text = String(Qty)
UIView.animateWithDuration(0.5, animations: {
cell.drinkNameLabel.alpha = 1
cell.drinkVesselLabel.alpha = 1
cell.drinkSizeLabel.alpha = 1
cell.drinkPriceLabel.alpha = 1
cell.drinkQtyLabel.alpha = 1
})
cell.addDrinkButton.tag = indexPath.row
cell.drinkQtyLabel.tag = indexPath.row
cell.addDrinkButton.addTarget(self, action: Selector("addDrink:"), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func addDrink(sender:UIButton){
println("Added drink in row \(sender.tag)"
//update cell.drinkQtyLabel.text
}
}
Pseudo code of what I want might look something like:
func addDrink(sender: UIButton){
for cell at indexPath(sender.tag){
cell.drinkQtyLabel.text = int(cell.drinkQtyLabel.text) + 1
}
}
But I know this isn't a solution!