FirebaseUi - Cast value of UITableViewCell to custom class in swift - ios

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

Related

Adding cell to Table view when button tapped in swift

i have to views in my app. mainMeasurements and historyMode. in main measurement, user can take photo and measure distance, speed and time. I need that app will save that measured datas in TableView in a historyMode, when reset button tapped (like a recents in a phone app). i already prepared datas and commented them. it should add cells to table view with image and "lastTop..." datas and show old ones too
class mainMeasurements: UIViewController, CLLocationManagerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var speedArray: [String] = []
var arrayOfImages: [UIImage] = []
var distanceArray: [Int] = []
var lastTopDistance: String?
var lastTopSpeed: String?
var lastTopTime: String?
#IBOutlet weak var takePhoto: UIButton!
#IBOutlet weak var mountainImage: UIImageView!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var distanceLabel: UILabel!
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var resetButtonLabel: UIButton!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "switchToHistory"{
// let historyModeVc = segue.destination as! historyMode
// historyModeVc.memoryPhoto = arrayOfImages.last
// historyModeVc.historySpeedLabelMM = lastTopSpeed
// historyModeVc.historyDistanceLabelMM = lastTopDistance
// historyModeVc.historyTimerLabelMM = lastTopTime
}
}
#IBAction func resetDidTouch(_ sender: UIButton) {
arrayOfImages.append(mountainImage.image!)
self.lastTopSpeed = self.maxSpeed
self.lastTopDistance = self.distanceLabel.text
self.lastTopTime = self.timerLabel.text
self.arrayOfImages.append(self.mountainImage.image!)
}
}
AND
import UIKit
class historyMode: UIViewController {
var memoryPhoto: UIImage?
var historyTimerLabelMM: String?
var historySpeedLabelMM: String?
var historyDistanceLabelMM: String?
override func viewDidLoad() {
super.viewDidLoad()
// historyDistanceLabel.text = historyDistanceLabelMM
// historySpeedLabel.text = historySpeedLabelMM
// historyTimerLabel.text = historyTimerLabelMM
// historyImage.image = memoryPhoto
}
If I'm not mistaken, you're inquiring about how to display your data on a table view?
You can parse your data like this and pass it to historyMode:
struct YourData {
var arrayOfImages: [UIImage]
var lastTopDistance: String?
var lastTopSpeed: String?
var lastTopTime: String?
}
Create a custom cell:
class CustomCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// configure your cell UI
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func set(with data: YourData) {
// initialize your properties with the data you provide
}
}
Register your custom cell with historyMode and pass your data to the custom cell:
var yourDataArray: [YourData]!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
let yourData = yourDataArray[indexPath.row]
cell.set(with: yourData)
return cell
}
Add new element to your dataSource and use NotificationCentre to call tableView.reloadData() in historyMode

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

Thread 1: EXC_BAD_INSTRUCTION Error

Hi I've been trying to code a UITableView Custom Cell and when I ran it I keep getting the error "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)" on this line of code:
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell
My full code is below
In ViewController.swift:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var mytableview: UITableView!
var arrayOfPersons: [Person] = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setUpPersons()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setUpPersons()
{
var person1 = Person(name: "Anna", number: 60, imageName: "testingimage.jpg")
var person2 = Person(name: "Joes", number: 10, imageName: "testingimage2.jpg")
arrayOfPersons.append(person1)
arrayOfPersons.append(person2)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return arrayOfPersons.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell
if indexPath.row % 2 == 0
{
cell.backgroundColor = UIColor.darkGrayColor()
}
else
{
cell.backgroundColor = UIColor.blackColor()
}
let person = arrayOfPersons [indexPath.row]
cell.setCell(person.name, rightlabelInt: person.number, imageName: person.imageName)
return cell
}
}
In CustomCell.Swift:
import UIKit
class CustomCell: UITableViewCell {
#IBOutlet weak var leftlabel: UILabel!
#IBOutlet weak var rightlabel: UILabel!
#IBOutlet weak var myimageview: UIImageView!
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
}
func setCell(leftlabelText: String, rightlabelInt: Int, imageName: String)
{
self.leftlabel.text = leftlabelText
self.rightlabel.text = String(rightlabelInt)
self.myimageview.image = UIImage(named: imageName)
}
}
And in Person.Swift:
import Foundation
class Person {
var name = "name"
var number = 0
var imageName = "blank"
init(name: String, number: Int, imageName: String)
{
self.name = name
self.number = number
self.imageName = imageName
}
}
I am not sure what the error means or if I made a mistake somewhere. Any help would be great!
Thanks!
I had a similar problem with a class inheriting from UITextView. The solution turned out to be I needed to override all the init methods.
override init()
{ super.init() }
override init(frame: CGRect, textContainer: NSTextContainer)
{ super.init(frame: frame, textContainer: textContainer) }
override init(frame: CGRect)
{ super.init(frame: frame) }
required init(coder aCoder: NSCoder)
{ super.init(coder: aCoder) }

iOS 8 UITableViewCell Class Compiler Error since Xcode Beta5

I'm already using Swift and since the update of Xcode beta4 to beta5 there are two compiler errors in the following code.
1st: init function - Error: "Overriding declaration requires an 'override' keyword" --> Solved
2nd: class FeedTableCell : UITableViewCell - Error: "Class 'FeedTableCell' does not implement its superclass's required members"
I couldn't find the required members in the documentation and other research - does anyone know what to do?
Code:
import UIKit
class FeedTableCell : UITableViewCell{
#IBOutlet var userLabel: UILabel!
#IBOutlet var hoopLabel: UILabel!
#IBOutlet var postLabel: UILabel!
func loadItem(#user: String, hoop: String, post: String) {
userLabel.text = user
hoopLabel.text = "#"+hoop
postLabel.text = post
}
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
}
}
I resolved this issue by adding required init(coder aDecoder: NSCoder!) method:
class FeedTableCell : UITableViewCell{
#IBOutlet var userLabel: UILabel!
#IBOutlet var hoopLabel: UILabel!
#IBOutlet var postLabel: UILabel!
func loadItem(#user: String, hoop: String, post: String) {
userLabel.text = user
hoopLabel.text = "#"+hoop
postLabel.text = post
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
}
}
The other solution is remove init method at all:
class FeedTableCell : UITableViewCell{
#IBOutlet var userLabel: UILabel!
#IBOutlet var hoopLabel: UILabel!
#IBOutlet var postLabel: UILabel!
func loadItem(#user: String, hoop: String, post: String) {
userLabel.text = user
hoopLabel.text = "#"+hoop
postLabel.text = post
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool){
super.setSelected(selected, animated: animated)
}
}

Resources