Table view not working - ios

I get an error by the IBoutlet of tableView saying "cannot override with stored property "tableView"". I'm not quite sure what this means, any type of help will be appreciated.
import UIKit
class UsersViewController: UITableViewController {
#IBOutlet var tableView: UITableView!
var users = ["Marc", "Mark" , "Louise", "Ahinga", "Amossi", "Kalenga"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return 3
}
override func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! userCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}

A UITableViewController comes with a UITableView built in, aptly named tableView. In the code you pasted you are adding an extra UITableView to it and giving it the same name. That's what the compiler is complaining about.
In theory, when you are using a plain vanilla UITableViewController from the storyboard you should not link the table to an IBOutlet. That's taken care of.
If you are building something more sophisticated that needs an extra UITableViewController on top of the one that the class provides in storyboard, then you should name it differently.

you have to set as it's delegate and dataSource in viewDidLoad() like this
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}

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.

Loading a Custom UITableViewCell from nib to a UIViewController in Swift

I'm currently working on an exercise swift program, one that requires CoreData results to be displayed on a table.
I've structured my app such that the storyboard itself doesn't contain any UI elements, only views (with an accompanying UIViewController class), which then loads custom nibs / xibs (also accompanied by a UIView Subclass). In this case, the xib contains a UITableView, and a separate xib contains the cell.
TableView Class:
import UIKit
protocol SkillsViewDelegate{
}
class SkillsView: UIView {
var delegate : SkillsViewDelegate!
#IBOutlet weak var skillsTable: UITableView!
}
TableView Controller:
import UIKit
import CoreData
class SkillsViewController: UIViewController, SkillsViewDelegate, UITableViewDataSource, UITableViewDelegate {
var displaySkillsView : SkillsView!
var displaySkillsCell : SkillViewCell!
let textCellIdentifier = "skillViewCell"
override func viewDidLoad() {
super.viewDidLoad()
self.displaySkillsView = UIView.loadFromNibNamed("SkillsView") as! SkillsView
self.displaySkillsView.delegate = self
self.view = self.displaySkillsView
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK : -Table view delegates
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
return cell
}
}
TableCellView Class:
import UIKit
protocol SkillsViewCellDelegate{
}
class SkillViewCell: UITableViewCell {
var delegate : SkillsViewCellDelegate!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
#IBOutlet weak var skillName: UILabel!
#IBOutlet weak var skillRank: UILabel!
}
I can't figure out how to call the cell xib into the controller. I would guess it's something like this problem, but the difference is the guy is using a UITableViewController. I tried adding that as well, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles.
What I Tried:
I tried adding UITableViewController up top, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles.
Any suggestions?
You can just register your SkillsViewCell in your SkillsViewController
Obj C
-(void)viewDidLoad {
[super viewDidLoad];
UINib *cellNib = [UINib nibWithNibName:#"NibName" bundle:nil];
[self.skillsTable registerNib:cellNib forCellReuseIdentifier:textCellIdentifier];
}
Swift 2
override func viewDidLoad() {
super.viewDidLoad()
let nibName = UINib(nibName: "NibName", bundle:nil)
self.skillsTable.registerNib(nibName, forCellReuseIdentifier: textCellIdentifier)
}
Swift 4
override func viewDidLoad() {
super.viewDidLoad()
let nibName = UINib(nibName: "NibName", bundle:nil)
self.skillsTable.register(nibName, forCellReuseIdentifier: textCellIdentifier)
}
Then only inherit from UIViewController not UITableViewController
To call methods specific to your custom cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SkillViewCell
cell.skillName.text = "Some text"
cell.skillRank.text = "Some text"
return cell
}
I think you will need to use
func registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)
in viewDidLoad
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/registerNib:forCellReuseIdentifier:

Why my simple tableview app won't work?

Iv'e made a code for a simple tableview app and with textfield and a button.
When I click on the button it adds to the array but do not show it on the table view.
What can I do to see it?
Here is the code:
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
#IBOutlet weak var textfieldd: UITextField!
#IBOutlet var tasksTable:UITableView!
var toDoList:[String] = []
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 toDoList.count
}
#IBAction func Add(sender: AnyObject) {
toDoList.append(textfieldd.text)
println(toDoList)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = toDoList[indexPath.row]
return cell
}
}
You've updated your data source (the array), but not the actual display (the table view). You should call the reloadData() function whenever you want to update your table view:
tasksTable.reloadData()
You need to reload the TableView after you've added the new element to the list.
You can do so with the reloadData method.
tasksTable.reloadData()
You are not applying the UITableviewDataSource protocol to your class, just the delegate.
Add it and set the datasource for the table to the class?

Table view displays limited number of cells?

I want to display 128 cells in the table view. However, due to some reason the table view displays a maximum of five cells. I checked the number of rows returned by the code, it is greater than 5. So I am sure that part is correct. Also, I have written code for custom cell. Does this contribute to this behavior? If yes, what should I do ? If no, what am I doing wrong ?
/* Custom cell code */
class myCustomCell: UITableViewCell{
#IBOutlet var myTitle: UILabel!
#IBOutlet var mySubtitle: UILabel!
convenience required init(reuseIdentifier: String!){
self.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier )
}
}
/* code for table view */
import Foundation
import UIKit
import CoreLocation
class TableViewController: UITableViewController{
var rowNumber: String!
override func viewDidLoad() {
super.viewDidLoad()
//println("Count is : \(dataArray.count)")
// 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.
}
override func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1;
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
//println("Here Count is : \(dataArray.count)")
return dataArray.count
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellId = "cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellId) as? myCustomCell
//UITableViewCell
if nil==cell {
cell = myCustomCell(reuseIdentifier: cellId)
}
if let ip = indexPath{
var dict: NSDictionary! = dataArray.objectAtIndex(indexPath.row) as NSDictionary
cell!.myTitle.text = dict.objectForKey("name") as String
}
return cell
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath: NSIndexPath){
//println("Clicked \(didSelectRowAtIndexPath.row)")
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if(segue.identifier == "centerDetails"){
var svc = segue!.destinationViewController as CellClickController
var selectIndex = self.tableView.indexPathForCell(sender as UITableViewCell)
svc.cellIdx = selectIndex.row
}
}
}
Thanks!
The UITableView method dequeueReusableCellWithIdentifier is why only 5-ish cells are instantiated at one time. The UITableView only creates enough UITableViewCell objects are to fill the screen. When one scrolls off and is no longer in view, it is queued for reuse. If you are scrolling quickly, it may create more cells than the screen needs, but generally it will use just enough to cover the screen.
You can create a new UITableViewCell for each index path you display. However it will go out of scope when it scrolls offscreen unless you retain the object reference yourself. You can do this by adding it to an array that your class manages.

Resources