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)
}
}
Related
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 am trying to make a protocol in a UITableViewCell class but when i declare my delegate, I get an error in both required init?(coder aDecoder: NSCoder) and override init(style: UITableViewCellStyle, reuseIdentifier: String?)
Error : - Property 'self.delegate' not initialised at super.init
This is my subclass:-
import UIKit
protocol tableCellBtnActionDelegate{
func removeRowAtIndex(_: Int)
}
class FriendsListTableViewCell: UITableViewCell {
#IBOutlet weak var friendListAddBtn: UIButton!
var usersId : String!
var buttonText : String!
var indexPath : Int!
let delegate : tableCellBtnActionDelegate!
override func awakeFromNib() {
super.awakeFromNib()
friendListAddBtn.layer.cornerRadius = 4
friendListAddBtn.layer.backgroundColor = UIColor(red: 121.0/255.0, green: 220.0/255.0, blue: 1, alpha: 1).CGColor
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
}
Well you haven't initialize it, so the compiler gives you a warning.
I would advise you to modify the delegate to be optional and set your delegate whenever you need it.
var delegate : tableCellBtnActionDelegate?
You should also handle the case where delegate is not set(nil).
Change
let delegate : tableCellBtnActionDelegate!
to
var delegate : tableCellBtnActionDelegate!
or you can't set value to delegate property ever
You got a warning because you are not initialising the delegate property. It should actually be a weak property, or you will retain a reference to the delegate object, which you usually don't want to do. So the best approach would be:
weak var delegate : tableCellBtnActionDelegate?
And then you have to use it like this:
self.delegate?.notifyAboutSomething()
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
}
}
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) }
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have custom cell in table view. When I am running my code I am getting
below error
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
My code is below:
class viewCompanyResultController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var uiTblCompanyResultView: UITableView!
var companyName=""
var countryCode=""
var townCode=""
var element=""
var resultDict: NSDictionary=[:]
var comapniesArr:[Company] = [Company]()
override func viewDidLoad() {
super.viewDidLoad()
self.uiTblCompanyResultView.delegate=self
self.uiTblCompanyResultView.dataSource=self
self.comapniesArr.append(Company(orgName: "cbre", orgCode: "orgCode2", imageName: "3.png"))
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return self.comapniesArr.count
}
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
let cell: CompantResultCell = tableView .dequeueReusableCellWithIdentifier("cell") as CompantResultCell
let companyResult=comapniesArr[indexPath.row]
println("dgfdf \(companyResult.orgName)")
cell.setCompanyCell(uiImgConComp: companyResult.imageName,lblCompanyName: companyResult.orgName)
return cell
}
}
Custome Cell Class
import UIKit
class CompantResultCell: UITableViewCell {
#IBOutlet var uiImgConComp: UIImageView!
#IBOutlet var lblCompanyName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
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)
}
func setCompanyCell(imageName: String,lblCompanyName: String)
{
self.uiImgConComp.image=UIImage(named: imageName)
self.lblCompanyName.text=lblCompanyName;
}
}
Custom cell class looks perfect!
You need to unwrapped custom cell object in tableView:(_cellForRowAtIndexPath:) method like below:
func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
var cell: CompantResultCell! = tableView .dequeueReusableCellWithIdentifier("cell") as? CompantResultCell
if (cell == nil) {
cell = CompantResultCell(style: UITableViewCellStyle.Default, reuseIdentifier:"cell")
}
let companyResult=comapniesArr[indexPath.row]
println("dgfdf \(companyResult.orgName)")
cell.setCompanyCell(uiImgConComp: companyResult.imageName,lblCompanyName: companyResult.orgName)
return cell
}
Edit:
Also unwrapped UIImage object in setCompanyCell method:
func setCompanyCell(imageName: String,lblCompanyName: String) {
var cellImage = UIImage(named: imageName as String!) as UIImage!
self.uiImgConComp.image = cellImage
self.lblCompanyName.text=lblCompanyName;
}
Calling the function behaviour is different in Swift, not like in Objective C. Call this function as below line:
cell.setCompanyCell(companyResult.imageName, lblCompanyName: companyResult.orgName)