UILabel will not show up on UITableViewCell - swift

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

Related

Did select show check/image using custom tableview cell

Ok so this seems like a really simple question, but I can't seem to solve it efficiently or find anything on the web that has the correct answer. So here goes.
I have a tableViewController with a prototype cell running off a custom class. Inside the prototype cell I have an image of a checkmark which I want to display when the user selects the cell and hide the image when another cell gets selected. I've got an approach working but it requires me to reloadData on the table which seems really inefficient. So there must be a better way right?
My code...
//CUSTOM CLASS (trimmed code down to just show relevant code)
class GoalsTableViewCell: UITableViewCell {
#IBOutlet weak var gNameLabel: UILabel!
#IBOutlet weak var gIsSelectedImage: UIImageView!
}
}
//TABLE VIEW CONTROLLER (trimmed it down to just show relevant code)
class GoalsTableVC: UITableViewController {
var selectedGoalId = ""
var selectedInd = 0
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let gCell = tableView.dequeueReusableCell(withIdentifier: "gCell", for: indexPath) as! GoalsTableViewCell
gCell.gNameLabel.text = goalsData[indexPath.row].gName
if indexPath.row == selectedInd {
gCell.gIsSelectedImage.isHidden = false
} else {
gCell.gIsSelectedImage.isHidden = true
}
return gCell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedGoalId = goalsData[indexPath.row].id!
selectedInd = indexPath.row
tableView.reloadData()
}
}
It's possible to reload only the affected rows
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedGoalId = goalsData[indexPath.row].id!
let indexPathsToReload = [indexPath, IndexPath(row: selectedInd, section: 0)]
selectedInd = indexPath.row
tableView.reloadRows(at: indexPathsToReload, with: .automatic)
}

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

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
}

Thread 1 EXC error

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

UIlabel text alignment in UITableViewCell not working

Sorry for the Chinese characters, but the UILabel text alignment displayed as right side NO MATTER which option (left, center and etc.) it is set. Please help.
// This is the function that populates the cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! WordLibraryTableViewCell
let key = Int(dataSource[indexPath.section][indexPath.row])
let value = levelDict[key!]! as String
cell.wordLibLabel.text = value
return cell
}
// This is the cell class
class WordLibraryTableViewCell: UITableViewCell {
#IBOutlet weak var bookimageView: UIImageView!
#IBOutlet weak var wordLibLabel: UILabel!
}