Swift TableView for iOS - ios

I have referred to this question, and tried to implement the various answers. I'm doing something dumb, as cellForRow is not being called, and I'm just getting a blank tableView. Are there other data source or delegate methods that I must override?
Thanks
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//tableView.delegate = self
//tableView.dataSource = self
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
//tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
println("cellForRowAtIndexPath")
println()
//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell!.textLabel.text = "Text Label"
cell!.detailTextLabel.text = "Detail Text Label"
return cell
}
}
UPDATE - The following appears to work.. Thanks for the help!!
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//tableView.delegate = self
//tableView.dataSource = self
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
//tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
println("cellForRowAtIndexPath")
println()
//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell!.textLabel.text = "Text Label"
cell!.detailTextLabel.text = "Detail Text Label"
return cell
}
}

Here is working code. No need to register cell in -viewDidLoad(). Also UITableView is added as subview in Storyboard.
If someone has this kind of requirement.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell?.textLabel?.text = NSString(format: "%d", indexPath.row) as String
return cell!
}
}

You need to connect Delegate DataSource of TableView, both are connected with the tableview then can only NumberOfRowsInSection and CellForRowAtIndexpath called.

Related

Add subview of xib tableview cell (swift)

I have created a tableview and xib tableviewcell. i use the xib tableviewcell by adding subview. however it seems lost the function of func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
i want this custom tableviewcell inherit the normal cell in my tableview.
how to do that??
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var myTableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
myTableView.dataSource = self
myTableView.delegate = self
myTableView.backgroundColor = UIColor.whiteColor()
myTableView.frame = CGRectMake(20, 50, 250, 400)//Optional for table size
myTableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "myIdentifier")
self.view.addSubview(myTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier") as! MyTableViewCell
myCell.myString1.text = "Row \(indexPath.row)"
myCell.myString2.text = "String \(indexPath.row)"
return myCell
}
}
You'll want to inherit from UITableViewController, and not UIViewController. As such:
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableview.delegate = self
self.tableview.backgroundColor = UIColor.whiteColor()
self.tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "myIdentifier")
)

Why doesn't tableview delegate work?

I have the following class in a singleview app. In IB, I have connected the datasource and datadelegate to the tableview. Is there something I'm doing wrong that none of the tableview delegates fire?
The cell identifier is set in IB.
import UIKit
class ViewController: UITableViewController {
let myarray = ["item 1", "item 2", "item 3", "item 4"]
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.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "test")
cell.textLabel?.text = myarray[indexPath.item]
return cell;
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
}
There is already a prototype cell in IB.
One other thing, viewDidLoad() doesn't even fire.
You are supposed to dequeue cells in cellForRowAtIndexPath. In storyboard, add a UITableViewCell to the table if there isn't one already, and you should see a prototype cell. Make sure that the identifier of the prototype is "test" like it is in your code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("test") as UITableViewCell
cell.textLabel?.text = self.myarray[indexPath.row]
return cell
}

UITableView run error in xCode 6.1 IOS 8

Please see my code below, runs in Xcode 6.1 IOS 8 failed, it report error below:
Type "ViewController" doesn't conform to Protocol "UITableViewDataSource"
Could anyone help to have a look what is the problem is? Thank you!
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
var items: NSMutableArray = ["row 0","row 1","row 2","row 3","row 4"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.frame = CGRectMake(10, 10, 300, 530)
tableView.backgroundColor = UIColor(red: 90/255, green: 31/255, blue: 10/255, alpha: 0.2)
// tableView.style = UITableViewStyle.Grouped
tableView.rowHeight = 50
tableView.separatorColor = UIColor.blueColor()
tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
//self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// tableView.delegate = self
// tableView.dataSource = self
self.view.addSubview(tableView)
}
//UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
//println("Hello \(indexPath.row)")
var cell = tableView.dequeueReusableCellWithIdentifier("my cell id") as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "my cell id")
}
//cell.textLabel?.text = listOfViewControllers[indexPath.row]
//cell!.textLabel?.text = items[indexPath.row] as String
//var s = items.objectAtIndex(indexPath.row) as? String
var s = items.objectAtIndex(indexPath.row) as String
cell!.textLabel?.text = s
return cell
}
//UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Issue is with this method signature, it is not similar to the one defined in the UITableViewDataSource
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
Change that to:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

creating table view using swift

I have been trying to create a table view using swift using xcode 6 and ios8 but I am not getting anything on my simulator,simply it showing empty(white).
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var myArr = ["one","two","three","four","five"]
#IBOutlet weak var table: 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 myArr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel?.text = self.myArr[indexPath.row]
return cell;
}
}
I have even tried by dragging some elements like button, switch, etc, and I am not getting anything on simulator. If I download and run any code its working.
Here my worked code, i dont understand what was wrong in previous one,but i have created a new project then it worked.
i just dragged a table view to viewcontroller and assigned delegate and datasource
then came to viewcontroller.swift and tried
import UIKit
class ViewController: UIViewController {
var items = ["a","b","c"]
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 self.items.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell:UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "myCell") as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
}
Is dataSource of table set in Interface builder? You can do it in code as well:
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
}
You are missing numberOfSectionsInTableView from the UITableViewDataSource protocol:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
Your 'table' delegate is not set. Add:
table.delegate = self
to your viewDidLoad above
table.dataSource = self
you missed delegate and dataSource please add following lines in viewDidLoad
table.delegate = self
and
table.dataSource = self

FirstViewController does not conform to Protocol

I wrote this in Xcode 6 (Swift) but it says "Type 'FirstViewController' does not conform to protocol 'UITableViewDataSource'" and won't let me build the program. Please help?
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
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.
}
//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return taskMGR.tasks.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->
UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
"test")
cell.textLabel?.text = taskMGR.tasks[indexPath.row].name
cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
return cell
}
}
As I wrote in the comments you might as well change the class to a UITableViewController subclass as it is basically the same as UIViewController + UITableViewDelegate + UITableViewDataSource (with a little bit of extra functionality included if you want it). It also has a UITableView property included "out of the box".
You will then end up with the following class:
class FirstViewController: UITableViewController {
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.
}
//UIViewTableDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return taskMGR.tasks.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"test")
cell.textLabel?.text = taskMGR.tasks[indexPath.row].name // You can remove ? when updating to XCode 6.1 / Swift 1.1
cell.detailTextLabel?.text = taskMGR.tasks[indexPath.row].desc
return cell
}
}
I rewrote your class to work. I deleted a couple of variables I did not need, but you can add them back. Key was to delete 'UITableViewDataSource' (you do not conform to this) and to unwrap the optional cell the way you wrote it. I prefer not to construct the cell that way, but that is another discussion. If you still have issues let me know.
import UIKit
class FirstViewController: UIViewController, UITableViewDelegate {
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.
}
//UIViewTableDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) ->
UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:
"test")!
return cell
}
}

Resources