Missing init codes in .swift files - ios

I have been trying to learn to do the UITableView Custom Cells and have been looking up examples on youtube. However they always come up with an error when I run them.
I was looking at this video https://www.youtube.com/watch?v=rIRvqRzOa-s and I noticed that when they first created a new .swift file it had this code:
init(style: UITableViewStyle){
super.init(style: style)}
When I created my .swift file it did not have this in it. I am wondering if this is why my tableviews are not running. I am currently using Xcode 6.1.1. I had tried to just type the code in as well but that didn't work either. Anyway to fix this or is it a bug?
EDIT:
so the full code I used for maintableviewcontroller.swift:
import UIKit
var ArrayObject = ArrayData()
class MainTableViewController: UITableViewController
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ArrayObject.MyArray().count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:MyCustomTableViewCell = tableView .dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as MyCustomTableViewCell
cell.LabelForDisplay.text = ArrayObject.MyArray().objectAtIndex(indexPath.row) as String
return cell
}
}
Mycustomtablecell.swift:
import UIKit
class MyCustomTableViewCell: UITableViewCell {
#IBOutlet var LabelForDisplay: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
and ArrayData.swift:
import UIKit
class ArrayData: NSObject {
func MyArray() -> NSMutableArray
{
var Arraydatas:NSMutableArray = ["Effect Works", "NPN Labs", "Alvin", "Varghese"]
return Arraydatas
}
}
the error I was getting was "thread 1 sigterm"

When you want to create an UITableViewController instead of selecting File > New > File... > Swift File
do File > New > File... > Cocoa Touch Class > Class: MainTableViewController > Subclass of: UITableViewController
This way you will have all the necessary code to start, no need to copy it from tutorials.

Related

UITableView always displays basic cell instead of custom cell in Swift

I spent hours of trying to fix this, but my simple app still displays the basic cell type instead of my prototype cell. I'm aware of using the identifier and registering after loading up the view, but it still displays the basic cells with just one label.
Here is my code so far:
My prototype is using this UITableViewCell:
class CoinTableViewCell: UITableViewCell {
#IBOutlet weak var coinIcon: UIImageView!
#IBOutlet weak var coinTitleLabel: UILabel!
#IBOutlet weak var holdings: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
UITableViewController:
class CoinTableViewController: UITableViewController {
var coins = ["Coin1","Coin2","Coin3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(CoinTableViewCell.self, forCellReuseIdentifier: "currency_cell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coins.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CoinTableViewCell! = self.tableView.dequeueReusableCell(withIdentifier: "currency_cell", for: indexPath) as! CoinTableViewCell
let coinName = coins[indexPath.row]
cell.coinTitleLabel?.text = coinName
return cell!
}
}
I would be so grateful if someone is able the help me out with this!
You are creating your custom cell directly on the tableview in the storyboard, right ?
If this is the case then you don't need to register the cell in your viewDidLoad as the storyboard takes care of that. You just deque it and it's good to go.
If you register it manually you just override what the storyboard did and end up getting a regular cell as the cell gets instantiated from the code instead of getting instantiated from the storyboard.
Cheers

How can I display multiple string values to multiple labels in a custom TableView Cell in swift ios?

var leadername = ["1","2","3","4"]
var districts = ["Delhi","Kerala"]
override func viewDidLoad() {
leadTableSetup()
super.viewDidLoad()
}
func leadTableSetup(){
LeadTableView.delegate = self
LeadTableView.dataSource = self
self.LeadTableView.register(UINib(nibName: "LeaderBoardTableViewCell", bundle: nil), forCellReuseIdentifier: "leadCell")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 14
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "leadCell") as! LeaderBoardTableViewCell
// Set text from the data model
cell.areaLbl.text = districts[indexPath.row]
cell.leaderNameLbl.text = leadername[indexPath.row]
return cell
}
I have declared two strings and I need to display these strings in the labels in my custom collection view cell that I have created. How can I achieve this? I need to display "leadername" string in one label and "districts" label in another label.
Go with this demo, Shared Demo
After the demo, If you still face any problem then let me know.
Now Listen Here
I think you need output something like this,
Follow the steps: -
Create a new viewcontroller(says, CustomTableVC) in your storyboard and one UITableView(give constraints and delegate to its own class), take outlet of UItableView (says, tblMyCustom)
Now press CLT+N for new file and do like this below image, Subclass - UItableViewCell and also tick on XIB option.
Open our xib file, add new UIView (says myView, as you see highted in below image), in this myView add two labels
Now take outlet of these two labels in its customCell class
class CustomTableCell: UITableViewCell {
#IBOutlet var lblLeaderNo: UILabel!
#IBOutlet var lblDistict: UILabel!
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
}
}
Now back to your Viewcontroller Class
import UIKit
class CustomTableVC: UIViewController , UITableViewDelegate, UITableViewDataSource{
#IBOutlet var tblMyCustom: UITableView!
var leaderno : [String]!
var distict : [String]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.tblMyCustom.register(UINib(nibName: "CustomTableCell", bundle: nil), forCellReuseIdentifier: "customCell")
self.leaderno = ["1", "2", "3", "4"]
self.distict = ["Delhi","Kerala", "Haryana", "Punjab"]
// above both array must have same count otherwise, label text in null
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return leaderno.count;
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var customCell: CustomTableCell! = tableView.dequeueReusableCell(withIdentifier: "customCell") as? CustomTableCell
customCell.lblLeaderNo.text = self.leaderno[indexPath.row]
customCell.lblDistict.text = self.distict[indexPath.row]
return customCell
}
}
above all is code of VC, it is not getting settle down here in single code format, I dont know why.
Now, follow these steps you get output as i show you image in starting of the procedure.

Can you help solve data.entry.init issue?

I'm getting the "thread 1: exc_bad_access (code=2, address=0x16fc0bfefe8) error. I have set up the iOS app to access a Table View Controller after logging in. It should display images. I have been following this tutorial http://shrikar.com/uitableview-and-uitableviewcell-customization-in-swift/ but no luck. This is my data.swift file where the error originates from:
import Foundation
class Data {
class Entry: Data {
let filename : String
init(fname : String) {
self.filename = fname
}
}
let products = [
Entry(fname: "placeholder1"), // <- Thread 1 error code shows in this line
Entry(fname: "placeholder2"),
Entry(fname: "placeholder3")
]
}
HomeTableViewController.swift
import UIKit
class HomeTableViewController: UITableViewController {
let data = Data()
override func viewDidLoad() {
super.viewDidLoad()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.products.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! HomeTableViewCell
let entry = data.products[indexPath.row]
let image = UIImage(named: entry.filename)
cell.bkImageView.image = image
return cell
}
HomeTableViewCell.swift
import UIKit
class HomeTableViewCell: UITableViewCell {
#IBOutlet weak var bkImageView: UIImageView!
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
}
}
Humbly asking for some assistance. Thanks in advance.
The issue is this line:
class Entry: Data {
Since Entry inherits from Data when you create an instance of Data the program goes into an infinite loop of initializing both the first member of products (an Entry) and its parent class, Data. Instead it should be this, as per the tutorial:
class Entry {

swift - Method does not override any method from its superclass & 'UITableView' does not have a member named 'viewDidLoad'

I am very new to swift and when I say new, I mean I just started this morning, I am having an issue I tried googling for but can't seem to find a solution.
I have this swift file:
import UIKit
class ProntoController: UITableView {
var tableView:UITableView?
var items = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
}
func viewWillAppear(animated: Bool) {
NSLog("Here")
let url = "API.php"
NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) { data, response, error in
NSLog("Success")
}.resume()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
return cell!
}
}
My problem is with this section of code:
override func viewDidLoad() {
super.viewDidLoad()
}
I get these two errors:
'UITableView' does not have a member named 'viewDidLoad'
Method does not override any method from its superclass
You need to subclass UITableViewController, you're subclassing UITableView

Swift: UITableView? does not have a member named reloadData

I'm doing a basic intro to Swift course, and keep getting stuck on this 'to-do list' app task.
I've had to put a ? after creating an IB OUtlet ( #IBOutlet var tasksTable:UITableView? ) to stop it from crashing, but this has meant that I've got the error 'UITableView? does not have a member named reloadData' after this function:
override func viewWillAppear(animated: Bool) {
tasksTable.reloadData()
Anyone know what's going on here? Apologies for the simple question - I'm pretty new to this.
p.s I'm using Swift 6.1
Full code is below.
Thanks in advance.
import UIKit
var toDoItems :[String] = []
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tasksTable:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
// 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 toDoItems.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = "Example"
return cell
}
override func viewWillAppear(animated: Bool) {
tasksTable.reloadData()
}
tasksTable in an optional, so you should use optional chaining and write tasksTable?.reloadData(). Since after awakeFromNib, tasksTable will always be non-nil, you can also type your outlet UITableView! to avoid this.
Write it like this:
override func viewWillAppear(animated: Bool) {
tasksTable?.reloadData()
}

Resources