Thread 1 EXC error - swift

I've been creating a firebase database but I have one error that I just can't get past. It says:
thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)'
It's probably something simple that I'm just looking past. Any Ideas?
Here's the code & the error is on the line of code:
//
// Database.swift
// intern
//
// Created by Lani Daniels on 8/20/17.
// Copyright © 2017 Lani Daniels. All rights reserved.
//
import UIKit
import Firebase
import FirebaseDatabase
struct PostStruct {
let title: String
let message: String
}
class DatabaseViewController: UITableViewController {
var posts: [PostStruct] = []
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let label1 = cell.viewWithTag(1)as! UILabel
label1.text=posts[indexPath.row].title
// ERROR BELOW:thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)
let label2 = cell.viewWithTag(2)as! UILabel
label2.text=posts[indexPath.row].message
return cell
}
}

For the first error:
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
Change tableview to tableView
For the second one:
It seems like you're trying to access to the outlet that might be not a UILabel. check this by making optional biding like:
let label2 = cell.viewWithTag(2) as? UILabel
and check that label using breakpoint. It might be nil.
Also it is better to use a custom cell class with two labels as outlets and then dequeue that custom cell in the cellForRowAt method:
class CustomCell: UITableViewCell {
#IBOutlet weak var firstLabel: UILabel!
#IBOutlet weak var secondLabel: UILabel!
}
And then
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? CustomCell
cell?.firstLabel.text=posts[indexPath.row].title
cell?.secondLabel.text=posts[indexPath.row].message
return cell!
}
And don't forget to set that custom class for the cell in the interface builder

Related

Connect Prototype Cells to View Controller [Swift 4]

I am new to programming and currently working on a newsfeed like app. I had a normal Table view up and running fine, but want to try it now with a costume cell type. So I created one and thought connecting the labels the usual way would be perfectly fine, but I was wrong. So I am wondering how I can let my Text label connect to my view controller, so I can use my custom cell.
class ViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var newsfeedTableView: UITableView!
var ref: DatabaseReference!
var posts = [String]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (posts.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
//here is where I need the custom label to get the posts
cell.textLabel?.text = posts[indexPath.row]
cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 18.0)
return cell
}
}
Create subclass of UITableViewCell and connect IBOutlets to this class
class YourCell: UITableViewCell {
#IBOutlet weak var customLabel: UILabel!
...
}
don't forget to set class of your prototype cell in storyboard:
then in cellForRowAt data source method downcast your dequeued cell as YourCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! YourCell
then you have access to YourCell outlets
cell.customLabel.text = "SomeText"
...
I'm assuming that you are using Storyboard.
First of all, you should understand that there is little difference when you use own custom table cell. In that case, in the method "cellForRowAtIndexPath", after dequeue your cell, you just have to typecast table cell like 'as! YourCustomTableCellClass'. After this line, you can access each property of this class.
First, design your table cell on Storyboard whatever you want.
Now, make a subclass of UITableViewCell and assign this class to your prototype custom cell which you have designed on Storyboard. Also, don't forget to set "reuse identifier in Storyboard table cell.
Then connect your outlets with custom cell class from Storyboard.
Now you can use code like this:
class YourTableCellClass: UITableViewCell {
// I'm using these outlets as a sample, but you have to connect them from Storyboard.
var leftTextLabel: UILabel!
var rightTextLabel: UILabel!
}
class YourTableController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - TableView Delegate & DataSource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // return your number of rows here...
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100 // return the height for the row here.....or you can set height from Storyboard if you have fix height of all rows.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellIdentifier", for: indexPath) as! YourTableCellClass
cell.leftTextLabel.text = "Text" // Set text here....
cell.rightTextLabel.text = "Text" // Set text here....
return cell
}
}

Custom TableViewCell with SwipeCellKit

I have a problem with the understanding of the pod SwipeCellKit.
I have this simple setup (Firebase for retrieving the data)
On my :
class MyFriendsViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var MyFriendsTableview: UITableView!
var ref: DatabaseReference!
var MyFriendslist = [MyFriendsModel]()
And I have my cellForRowAt like this :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends", for: indexPath) as! MyFriendsTableViewCell
// let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends") as! SwipeTableViewCell
// cell.delegate = self
When I try to implement the swipeCellKit, I loose my references from my MyFriendTableViewCell (UIimage,label references) if I put :
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends") as! SwipeTableViewCell
cell.delegate = self
Anyone knows how to unblock this ?
Thanks!
Just change this:
class MyFriendsTableViewCell: UITableViewCell { ...
To this:
class MyFriendsTableViewCell: SwipeTableViewCell { ...
We've just inherited from SwipeTableViewCell class. After this change, you will be able to use your MyFriendsTableViewCell properties and SwipeTableViewCell properties.
Updated code:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomFriends", for: indexPath) as! MyFriendsTableViewCell
cell.delegate = self
...
cell.yourMyFriendsTableCellProperty = ...
return cell
}

UILabel will not show up on UITableViewCell

For some reason, UILabels do not show up when building the project on simulator (and no errors are reported by Xcode).
------- This is the code -------
import UIKit
class Cursos_PUC: UITableViewCell {
#IBOutlet weak var Curso_1: UILabel!
}
class Cursos_PUC_TableView: UITableViewController {
var cursos = ["Arquitetura", "Culinária", "Ciência da Computação"]
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! Cursos_PUC
let nomeCursos = cursos[indexPath.row]
cell.Curso_1?.text = nomeCursos
return cell
}
}
Are you using storyboard or Code??
if using storyboard then i think your label outlet is not connected to the label.if not then you must register your class to Table view cell

Setting imageView in cell

i am new to Swift and I have seen tons of guides on the topic. However in my case, it's working. I have a custom cell file:
class FileCell: UITableViewCell {
#IBOutlet weak var firstName: UILabel!
#IBOutlet weak var cellImage: UIImageView!
func updateImage(name: String) {
cellImage.image = UIImage(named: name)
}}
In the view controller I am using "willDisplay" function as follows:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
let user = users[indexPath.row]
if user.email.isEmpty {
//cell.cellImage.image = UIImage(named: "folder.png")
cell.updateImage(name: "folder.png")
} else {
//cell.cellImage.image = UIImage(named: "file.png")
cell.updateImage(name: "file.png")
}
}
where i try to change imageView in cell depending on the data incoming to cell. However, the images either don't display at all or cells are not showing up.
Thanks in advance for tips on what i am doing wrong.
You should not dequeue a new cell, since you already have one.
Just skip the first statement:
// let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
But one question: Why are you using the willDisplay delegate method? I would suggest to set up the cell in tableView(_:cellForRowAt:):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) {
let cell = tableView.dequeueReusableCell(withIdentifier: "FileCell") as! FileCell
let user = users[indexPath.row]
cell.firstName.text = user.name // or something
if user.email.isEmpty {
cell.updateImage(name: "folder.png")
} else {
cell.updateImage(name: "file.png")
}
return cell
}

Initialize optional UIActivityIndicatorView in Swift

I have no idea what I am doing wrong. In my TableViewController, I want each cell to have a UIActivityIndicator when tapped. So in my TableViewCell class I have the following:
#IBOutlet weak var loadingIcon: UIActivityIndicatorView!
And in TableViewController I have the following:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "taskCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? TodayTableViewCell else {
fatalError("The dequeued cell is not an instance of TodayTableViewCell.")
}
let task = tasks[indexPath.row]
cell.nameLabel.text = task.name
cell.descriptionLabel.text = task.description
cell.timeRangeLabel.text = task.timeRangeLabel
cell.currentTime.text = String(format:"%f", task.currentTime)
cell.totalTime.text = String(format:"%f", task.totalTime)
cell.taskIcon.image = task.icon
cell.loadingIcon = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
cell.loadingIcon.hidesWhenStopped = true
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt
indexPath: IndexPath){
let cell = (TodayTableViewCell)();tableView.cellForRow(at: indexPath)
print("tapped")
tasks[indexPath.row].isActive = !tasks[indexPath.row].isActive
if(tasks[indexPath.row].isActive) {
//Loading.start()
cell.loadingIcon.startAnimating()
}
else {
//Loading.stop()
cell.loadingIcon.stopAnimating()
}
}
I get fatal error: unexpectedly found nil while unwrapping an Optional value but I have no idea why because it looks like I am initializing it.
I get:
EXC_BAD_INSTRUCTION
Here is the storyboard:
Now back to the error I was getting earlier:
As you can see in the image, you can set that property on the storyboard. You can find on the right panel of your xcode.
If you still want to write that line you should connect the UIActivityIndicator to the IBOutlet