UIlabel text alignment in UITableViewCell not working - swift

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

Related

How to assign the specific endpoint to the specific selected cell

I have some data coming from the API and I am showing them in tableView cells, Each of those cell's have a "Download" button, I need to assign a fileID from API to the cell that the user has selected to Download.
I have already set up the button to a UITableViewCell class, I just need to have a idea how to make the button get the fileID depending on which cell is clicked since every cell has it's own fileID.
paymentsViewController class:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "paymentsCell", for: indexPath) as? paymentsModel else {return UITableViewCell()}
let currentInvoice = payments.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].payments
let currentInvoices = currentInvoice?[indexPath.row]
cell.priceLabelPayments?.text = "€\(currentInvoices?.amount ?? 0.00)"
cell.whenDateLabel?.text = "\(currentInvoices?.insertionDate?.convertToDisplayForm() ?? "")"
cell.fromSubLabel.text = "\(currentInvoices?.bank ?? "")"
cell.downloadButtonPayments.layer.cornerRadius = CGFloat(5)
cell.localizePaymentsModel()
return cell
}
}
paymentsModel class:
class paymentsModel: UITableViewCell {
#IBOutlet weak var cellContentViewPayments: UIView!
#IBOutlet weak var downloadButtonPayments: UIButton!
#IBOutlet weak var priceLabelPayments: UILabel!
#IBOutlet weak var fromSubLabel: UILabel!
#IBOutlet weak var whenDateLabel: UILabel!
func localizePaymentsModel() {
let defaults = UserDefaults.standard
let lang = defaults.string(forKey: changeLanguageKey.DefaultsKeys.keyOne)
downloadButtonPayments.setTitle(NSLocalizedString("Download", tableName: nil, bundle: changeLanguage.createBundlePath(lang: lang ?? "sq" ), value: "", comment: ""), for: UIControl.State.normal)
}
}
You are thinking about this wrong. It isn't the cell that has the fileID. When the user taps on a cell or a button, you should ask the table view (or collection view) for the indexPath it currently occupies, then use the IndexPath to look up the model data for that entry.
See my answer here:
Thant link shows code that lets you figure out the IndexPath for any view in a table view cell. You could use that code from your button's IBAction.

How to set multiple UITableview in single ViewController

I'm trying to make two TableViews in one ViewController and
having a trouble in cellForRowAt indexPath
I tried var cell:UITableviewCell? , return cell!
and now let cell = UITableViewCell() but it's still not working
#IBOutlet weak var ButtonA: UIButton!
#IBOutlet weak var ATV: UITableView!
#IBOutlet weak var ButtonB: UIButton!
#IBOutlet weak var BTV: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == self.ATV {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = Alist[indexPath.row]
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor(red:0.93, green:0.60, blue:0.49, alpha:0.5)
cell.selectedBackgroundView = bgColorView
}
if tableView == self.BTV {
let cell1 = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell1.textLabel?.text = Blist[indexPath.row]}
return cell
}
I want each button to have a different list

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

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

Swift, preventing tableview cell resueable

How to solve the table view data overlapped issue?
I have searched some Object C version answer, but it seems not working for Swift. I tried to get cell==nil, but it gives me an error
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell = myTable.cellForRowAtIndexPath(indexPath) as! PatientTableViewCell
if (cell){
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
if(cell != nil){
var subviews = cell.contentView.subviews
subviews.removeAll()
}
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}
After two-days struggling, I figure it out finally. The key to solve it is to find which cell is been resued. What I am doing now is give a var identifier to cell.
class PatientTableViewCell: UITableViewCell {
#IBOutlet weak var followupBtn: UIButton!
#IBOutlet weak var viewBtn: UIButton!
#IBOutlet weak var titleType: UILabel!
#IBOutlet weak var titleID: UILabel!
#IBOutlet weak var patientTypelbl: UILabel!
#IBOutlet weak var patientIDlbl: UILabel!
**var identifier = false**
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
var cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
if(cell.identifier == true){
cell = myTable.dequeueReusableCellWithIdentifier("patientCell") as! PatientTableViewCell
}
//config cell
cell.identifier = true //important
I hope it can help someone else. :)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let current_patient = patients[indexPath.row]
let cell: PatientTableViewCell! = tableView.dequeueReusableCellWithIdentifier("patientCell") as? PatientTableViewCell
//configure cell
let type = current_patient.enrollable_type
cell.patientTypelbl.text = type
return cell
}