didSelectRowAtIndexPath not getting called when row is pressed - ios

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

Related

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!

FirebaseUi - Cast value of UITableViewCell to custom class in swift

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
}
}

TableViewCell not updating

I have a TableView Controller with custom cells that I want reload with new data whenever the table view is loaded.
The problem I'm having is the data is reloading properly but the cell labels are not.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> JobTableViewCell{
jobSelect = Int(indexPath.row)
var cell: JobTableViewCell = tableView.dequeueReusableCellWithIdentifier("JobTableViewCell") as JobTableViewCell
if indexPath.row % 2 == 0{cell.backgroundColor = UIColor.lightGrayColor()
}
else {cell.backgroundColor = UIColor.whiteColor()}
var job = arrayOfJobs[indexPath.row]
var totalHours = job.monHours + job.tueHours + job.wedHours + job.thuHours + job.friHours + job.satHours + job.sunHours
cell.setCell(job.jobName, income: job.income, commute: job.commute, jobHours: totalHours)
return cell
}
Here is the custom cell -
import UIKit
class JobTableViewCell: UITableViewCell {
#IBOutlet var jobTitle: UILabel!
#IBOutlet var income: UILabel!
#IBOutlet var commute: UILabel!
#IBOutlet var jobHours: UILabel!
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 setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setCell(jobTitle: String, income: Int, commute: Int, jobHours: Int){
self.jobTitle.text = String(arrayOfJobs[jobSelect
].jobName)
self.income.text = String("Income: $\(arrayOfJobs[jobSelect].income) a month")
self.commute.text = String("Commute: \(arrayOfJobs[jobSelect].commute) hours away from home")
self.jobHours.text = String("Available hours:\(arrayOfJobs[jobSelect].monHours + arrayOfJobs[jobSelect].tueHours + arrayOfJobs[jobSelect].wedHours + arrayOfJobs[jobSelect].thuHours + arrayOfJobs[jobSelect].friHours + arrayOfJobs[jobSelect].satHours + arrayOfJobs[jobSelect].sunHours) hours a week")
}
}
I also have a second view controller set up that appears when a cell is selected -
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
jobSelect = Int(indexPath.row)
var JobSelector: jobSubView = storyboard?.instantiateViewControllerWithIdentifier("jobSubView") as jobSubView
self.presentViewController(JobSelector, animated: true, completion: nil)
self.rowSelection = Int(indexPath.row)
}
Here is the sub view -
import UIKit
class jobSubView: UIViewController {
#IBOutlet var jobTitle: UILabel!
#IBOutlet var income: UILabel!
#IBOutlet var commute: UILabel!
#IBOutlet var jobHours: UILabel!
#IBOutlet var qualifications: UILabel!
#IBOutlet var jobDescription: UITextView!
#IBOutlet var goBackButton: UIButton!
#IBOutlet var appluButton: UIButton!
#IBAction func applyButtonAction(sender: UIButton) {
override func viewDidLoad() {
super.viewDidLoad()
self.jobTitle.text = String(arrayOfJobs[jobSelect
].jobName)
self.income.text = String("Income: $\(arrayOfJobs[jobSelect].income) a month")
self.commute.text = String("Commute: \(arrayOfJobs[jobSelect].commute) hours away from home")
self.jobHours.text = String("Available hours: \(arrayOfJobs[jobSelect].monHours + arrayOfJobs[jobSelect].tueHours + arrayOfJobs[jobSelect].wedHours + arrayOfJobs[jobSelect].thuHours + arrayOfJobs[jobSelect].friHours + arrayOfJobs[jobSelect].satHours + arrayOfJobs[jobSelect].sunHours) hours a week")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
The labels on the cell and the subviews are drawing from the same data. When the tableview loads the first time both the cells and the sub view display correctly.
The subview loads with the correct data when a cell is selected, but when I return to the tableview the cell labels should update, but they are not. But the sub view labels are.
Any ideas?
So, I figured this out. You have to do self.tableView.reloadData() in viewWillAppear instead of viewDidLoad.

How to update a UILabel with UIButton in custom cell in Swift?

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!

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