I have a very simple tableview with a prototype cell in my project. I have no network operation and the data in the tableview cell is populated using hard coded values. Still the tableview is not smooth while scrolling. I am not able to figure out what is causing this lag. Code for the ViewController.swift file is as follows:
import UIKit
class ViewController: 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.
}
// MARK: Table view delegate and data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reviewCell", forIndexPath: indexPath) as! ReviewTableViewCell
// cell!.label.text = "\(indexPath.row)"
cell.reviewerNameLabel.text = "John Smith"
cell.dateLabel.text = "12 months ago"
cell.publisherNameLabel.text = "xyz publisher"
cell.reviewTextLabel.text = "qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmqwertyuiop1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnmqwertyuiop1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
return cell
}
}
I have set the tableview and prototype cell using the storyboard so for debugging simplicity, I have uploaded the project on github. You can get the complete project from here
Related
I have set up tableview many times but don't why is not it showing anything. I have added a tableview on my storyboard file, added reference on ViewController file and set datasource and delegate on storyboard. on viewDidLoad registered the cell and finally added reusableIdentifier on TableViewCell. Here is my code with details configuration :
import UIKit
struct ViewControllerConstants {
static let kCellIdentifier = "TableViewCell"
static let kNibName = "TableViewCell"
}
class ViewController: UIViewController {
var cellImageNames : [String] = ["image1","image2","image3","image4","image5","image6"]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.register(UINib.init(nibName: ViewControllerConstants.kNibName, bundle: nil), forCellReuseIdentifier: ViewControllerConstants.kCellIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.reloadData()
}
}
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellImageNames.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: ViewControllerConstants.kCellIdentifier, for: indexPath) as? TableViewCell {
cell.contentImageView.image = UIImage.init(named: cellImageNames[indexPath.row])
return cell
}
return UITableViewCell.init()
}
}
extension ViewController : UITableViewDelegate {
}
Try to remove identifiers for cells in storyboard.
Problem: Cell height was not defined on code. So it was creating cell with 0 height value .
Solution: Need to specify height of each row as below code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
I have a simple project, with UITableView added as subview of the current view, and external tableviewcontroller class that make a delegate and datasource.
The problem is that all work correctly excepted delegate didSelectedRowAtIndexPath, when I click in a single row, all tableview become white, here is the code:
MAIN CLASS:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let cont:Tbcontroller = Tbcontroller()
let tableViewInner = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width-(self.view.frame.width/6), height: 180))
tableViewInner.scrollEnabled = false
tableViewInner.separatorStyle = .None
tableViewInner.allowsSelection = true
tableViewInner.userInteractionEnabled = true
cont.type = 1
cont.setCurrentTableView(tableViewInner)
self.view.insertSubview(tableViewInner, atIndex: self.view.subviews.count+1)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
EXTERNAL CLASS DELEGATE DATASOURCE:
import UIKit
class Tbcontroller: UITableViewController {
var type:Int = 1
override func viewDidLoad() {
super.viewDidLoad()
}
func getCell() -> UITableViewCell{
let presentCell: UITableViewCell = UITableViewCell()
return presentCell
}
func setCurrentTableView(table:UITableView){
self.tableView = table
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
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 {
// #warning Incomplete implementation, return the number of rows
return 4
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = getCell()
cell.textLabel?.text = "ciao"
cell.textLabel?.font = UIFont.systemFontOfSize(13)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("clicked")
}
}
When I click, I do not see the output "clicked"
Your main problem is that TbController() get deallocated. dataSource and delegate properties on UITableView are weak. Therefor, because you only assign TbController to a local variable and to the weak properties, it will get deallocated.
Also, the UITableViewController already creates a TableView for you. It is not good practice to "recreate" it. I would rather make use of the TableView in the UITableViewController and add the UITableViewController's view as a subview to your UIViewController's view.
To fix, you need an instance variable for TbController.
Sample code with ivar for TbController and making use of Tbcontroller's UITableView.
import UIKit
class ViewController: UIViewController {
var cont: Tbcontroller = Tbcontroller()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cont.type = 1
self.view.addSubview(cont.view)
}
}
import UIKit
class Tbcontroller: UITableViewController {
var type:Int = 1
override func viewDidLoad() {
super.viewDidLoad()
tableView.scrollEnabled = false
tableView.separatorStyle = .None
tableView.allowsSelection = true
tableView.userInteractionEnabled = true
self.tableView.delegate = self
self.tableView.dataSource = self
}
func getCell() -> UITableViewCell{
let presentCell: UITableViewCell = UITableViewCell()
return presentCell
}
// MARK: - Table view data source
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 {
// #warning Incomplete implementation, return the number of rows
return 4
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = getCell()
cell.textLabel?.text = "ciao"
cell.textLabel?.font = UIFont.systemFontOfSize(13)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("clicked")
}
}
thanks to Carien, it works fine, i changed uitableviewcontroller with:
: NSObject, UITableViewDataSource, UITableViewDelegate
import UIKit
class NewOrdersViewControllers: UIViewController,UITableViewDelegate,UITableViewDataSource {
var items = ["WE","Heart","Swift"]
#IBOutlet var tableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
It shows the error when I run the app. I have created a table view in swift using xib but it shows the following error :
NSArgumentException: '-[UIView tableView:numberOfRowsInSection:]:
unrecognized selector sent to instance 0x7c9a6ed0""
Make sure, you have given DataSource and Delegate to UITableView
tableView.delegate = self
tableView.dataSource = self
You define numberOfRows in 'numberOfSections`. Replace the below code with mine:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
Change to below:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
I think you have not set the delegate and the datasource for your tableView which happens to come form the xib . This is a common that people when they use UITableView.
Set the delegate and the datasource for your tableview using swift syntax for the following objective-c code
self.tableView.delegate = self
self.tableView.dataSource = self
These statements should be in your viewDidLoad method. Though the first is not compulsory if you dont want to handle the taps from the user on the cells. you have to write the second one for the display of information.
Tell me if something else is required.
Cheers.
The error message you're receiving indicates you've set the dataSource property of the tableView to the view, instead of the viewController to which you've added the tableView:numberOfRowsInSection: et al methods.
I have a question about responding to selections with a UITableView. I want to create a FAQ and trigger an action when the user selects a row. I'm just testing it right now, and set it to print a log message when the user selects one of the rows on the table view. However, in the simulator nothing happens.
I'm not sure what I am doing wrong. Can someone please help enlighten me?
Thanks in advance. Below is my code.
class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let iEdCafeVid = [
("temp", "temp"),
("temp", "temp"),
("temp", "temp") ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return iEdCafeVid.count //the number of spots in the array
}
//uses the numberOfRowsInSection method
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell //because creating as AnyObject.
//var cell = UITableViewCell() //instantiating class, but should use the dequeue option.
//need to add the prototype cell in the storyboard before the dequeue option will work.
//creating touple (desciptions of the cell content parts)
let (videoTitle, subtitle) = iEdCafeVid[indexPath.row] //index parth is the value (i.e. the array in this case) that we created in the numberOfRowsInSection method.
cell.textLabel.text = videoTitle
cell.detailTextLabel?.text = subtitle
//retrieve image
var cellImage = UIImage(named: "CellIcon")
cell.imageView.image = cellImage
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("user has selected a cell")
}
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.
}
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