variable cell is never mutated - ios

When I take var type variable then Xcode show warning
variable is never mutated
If I take let type variable then don't show any result!
import UIKit
class ViewController: UIViewController,UITableViewDataSource{
let people = [
("Pankaj","Dhaka"),
("Asish","Madaripur"),
("Anup","Narail")
]
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "People"
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let (personName , personLocation) = people[indexPath.row]
cell.textLabel?.text = personName
cell.textLabel?.text = personLocation
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

You cannot use a table view cell initialized with the default initializer UITableViewCell()
Reuse the cell, add an identifier (e.g. PeopleCell) in Interface Builder.
let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleCell", for: indexPath)
And make sure that datasource and delegate of the table view are connected in Interface Builder, too.

Related

Is it possible to give UITableViewCells each a unique ViewController?

To expand, I have a UITableView of two cells, and I want to give each of the cells, when tapped, their own unique view controller (as their functions will be different). I have scoured this website, as well as Google and could not quite find an answer. Here's the set up of my UITableView:
class ViewController: UITableViewController {
let functions = ["Rule of 72","Future Value", "Simple Interest"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.functions[indexPath.row]
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
}
Ultimately, the idea is to build an app that allows for the calculation of certain financial and economic equations. These equations will require different inputs and outputs (Rule of 72 will only need one UITextField, whereas Future Value will need four UITextField's).
I would suggest creating a enum confirming to CaseIterable. Also implement the func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath).
class ViewController: UITableViewController {
enum OptionTypes: CaseIterable {
case ruleOf72
case futureValue
case simpleInterest
var description: String {
switch self {
case .ruleOf72: return "Rule of 72"
case .futureValue: return "Future Value"
case .simpleInterest: return "Simple Interest"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let item = OptionTypes.allCases[indexPath.row]
cell.textLabel?.text = item.description
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return OptionTypes.allCases.count
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = OptionTypes.allCases[indexPath.row]
switch item {
case .ruleOf72:
navigateToRuleOf72ViewController()
case .futureValue:
navigateToFutureValueViewController()
case .simpleInterest:
navigateToSimpleInterestViewController()
}
}
private func navigateToRuleOf72ViewController() {}
private func navigateToFutureValueViewController() {}
private func navigateToSimpleInterestViewController() {}
}

Custom cell don't appear in tableView

My custom cell don't appear in my table View and I didn't find anything to answer that.
Here's my storyboard that contains the TableView :
This is my listController :
extension MatchListViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MatchCell", for: indexPath) as? MatchTableViewCell else {
return UITableViewCell()
}
let match = matchArray[indexPath.row]
cell.configure(nomDuMatch: match.matchName, scoreFinal: match.finalScore)
return cell
}
}
(I've configured the dataSourceDelegate by storyboard)
the customCell identifier is correct and I really don't understand why nothing appears at launch..
Feel free to ask me more pictures / infos !
Edit :
This is the result :
You need to implement
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100 // or any value
}
or use automatic cell and set constraints properly in IB as it seems that you have a constraints problem and set this in viewDidLoad
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension

Table cells in the second view are not displayed

The table cells contained in the first view are properly represented, but the table cells contained in the second view do not appear. I can’t understand why.
HelpController.swift
import UIKit
class HelpController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// MARK: - Initialize
#IBOutlet weak var menuTable: UITableView!
let helpMenu = ["a","b","c"]
override func viewDidLoad() {
super.viewDidLoad()
//datasource link
menuTable.delegate = self
menuTable.dataSource = self
self.menuTable?.tableFooterView = UIView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table View Data Source
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return helpMenu.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "HelpMenuCell")
cell.textLabel?.text = helpMenu[indexPath.row]
return cell
}
}
When i check it with print (helpmenu.count), it return 3. It seems to work well until numberOfRowsInSection, but cellForRowAt does not work.
and this is my first view InfosController.swift
// MARK: - Table View Data Source
// get cell count
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if UserDefaults.standard.value(forKey: "userid") != nil {
// sign in state
return memberMenu.count
} else {
// sign out state
return nonMemberMenu.count
}
}
// change cell text
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "InfosMenuCell")
if UserDefaults.standard.value(forKey: "userid") != nil {
// sign in state
cell.textLabel?.text = memberMenu[indexPath.row]
} else {
// sign out state
cell.textLabel?.text = nonMemberMenu[indexPath.row]
}
return cell
}
This is the code included in the first view that works properly. This works well, but I’m confused because it does not work well in the second view(HelpController).
ps.
The problem in HelpController, In controlle self.menuTable?.tableFooterView = UIView()
So if You are having footer view then You need to write the delegate function heightForFooterInSection and viewForFooterInSection
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
}
Make sure below things
Make sure you added Delegate & DataSource to tableView.
Check your helpMenu count is not equal to
Zero.
If numberOfRowsInSection returns Zero, then probably cellForRowAt
indexPath won't call.
Please check the method declaration once the correct syntax is
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell { }

TableView external dataSource and Delegate not loading

I have a tableview that is added programatically below that I want to hook up the delegate and dataSource to an external class. The code looks right however the tableview gets added to the view without getting the cell layout from the external class.
let tableView: UITableView = {
let dataService = ActivityDataService()
let tb = UITableView()
tb.tableHeaderView = nil
tb.tableFooterView = nil
tb.rowHeight = 50
tb.estimatedRowHeight = 50
tb.dataSource = dataService
tb.delegate = dataService
tb.register(ProfileActivitySubCell.self, forCellReuseIdentifier: "tableCell")
return tb
}()
Here is the activity service class:
class ActivityDataService: NSObject, UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! ProfileActivitySubCell
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 0
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
}
Thanks
When using a UITableView thats not in a storyboard or similar you need to register the cell with an identifier.
Depending on your UITableViewCell (if its a subclass and/or if you are using nibs or not)
You could use one of these methods:
open func register(_ nib: UINib?, forCellReuseIdentifier identifier: String)
open func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String)
Which is methods of UITableView
In your case probably something like this:
tb.register(ProfileActivitySubCell.classForCoder(), forCellReuseIdentifier: "tableCell")
1) Refactor the table view data source methods to a separate class
class IceCreamListDataSource: NSObject, UITableViewDataSource
{
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("IceCreamListCell", forIndexPath: indexPath)
return cell
}
}
2) In your controller class do this-:
class IceCreamListViewController: UITableViewController
{
let dataSource = IceCreamListDataSource()
// MARK: - View lifecycle
override func viewDidLoad()
{
super.viewDidLoad()
tableView.dataSource = dataSource
}
}
I managed to solve the issue. As the tableView was inside a collection view I had to use a storyboard object outlet. Finally inside the collection view cell I had to set the delegate and dataSource to the newly created object.
cell.tableView.dataSource = dataService
cell.tableView.delegate = dataService

How to add multiple images to to custom table view cell in swift ios?

I have an imageView in my custom table view cell. I have multiple images copied to Xcode.
Now I need to display these images in the tableview cell. I have decalared a leaderImage variable in which I have given the image names.
How can I achieve this?
This is what I have:
var leaderImage : [String]!
override func viewDidLoad() {
self.leadername = ["Oomen Chandy","PC.George","Veena George","M.Mukesh"]
self.areaName = ["Puthupally","Poonjar","Thrissur","Kollam"]
self.approvalrate = ["98%","94%","88%","95%"]
self.leaderImage = ["rahul","Anil","Manu",]
leadTableSetup()
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func leadTableSetup(){
LeadTableView.delegate = self
LeadTableView.dataSource = self
self.LeadTableView.register(UINib(nibName: "LeaderBoardTableViewCell", bundle: nil), forCellReuseIdentifier: "leadCell")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return leadername.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "leadCell") as! LeaderBoardTableViewCell
cell.leaderNameLbl.text = leadername[indexPath.row]
cell.areaLbl.text = areaName[indexPath.row]
cell.approvalLabel.text = approvalrate[indexPath.row]
cell.imageView = leaderImage[indexPath.row]
return cell
}
In cell cellForRowAt method
cell.imageView.image = UIImage(named: leaderImage[indexPath.row])
In your self.leadername contains 5 element but rest of all your array contains 4 or 3 element that's why you have problem occur.
To avoid Crash
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return leaderImage.count
}

Resources