Separators in custom UITableView are not shown - ios

I've created UITableView programmatically, but it doesn't show any separators. When I run the project I can see on Debug View Hierarchy that it exists, but i cannot understand why i cannot see it on simulator? Any of you knows what I'm missing or what is wrong with my code?
I'll really appreciate your help.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//🔵 Proerties
var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame: view.bounds)
tableView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = 100
tableView.separatorColor = UIColor.redColor()
tableView.separatorStyle = .SingleLine
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
view.addSubview(tableView)
}
// 🔵 UITableViewDataSource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let row = indexPath.row
cell.textLabel?.text = String(row)
return cell
}
// 🔵 UITableViewDelegate
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = UIColor.cyanColor()
}
}

Related

custom tableview cell doesn't appear

import UIKit
class anasayfaVC: UIViewController, UITableViewDataSource , UITableViewDelegate {
#IBOutlet weak var tableView1: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView1.delegate = self
tableView1.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FeedCell
cell.deneme.image = UIImage(named: "naruto")
return cell
}
}
did you register your tableview as
tableView.register(UINib(nibName: "FeedCell", bundle: nil), forCellReuseIdentifier: "Cell") ?

UITableViewCell gets deleted after scrolling

I am trying to create a ToDo list application. For this application, I require a custom UITableViewCell. I am trying to use my custom cell, however, when I run the app and try to scroll through the table view all the cells get deleted.
This is my code:
class ToDoListVC: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "TodoCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "customCell")
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return HomeVC.ToDoEvents.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! TodoCell
let eventName = HomeVC.ToDoEvents[indexPath.row].name
let eventTime = HomeVC.ToDoEvents[indexPath.row].time
let completion = HomeVC.ToDoEvents[indexPath.row].isComplete
cell.initialize(task: eventName!, length: eventTime!, completion: completion!)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
}
I am not sure what I am doing wrong. I went through some of the other threads, however, none of them solved my problem.

reloadData() calls cellForRowAtIndexPath for every cell

So I'm having this issue that when reloadData() is called after the initial API call, it calls willDisplayCell method which when the last cell is displayed will load more data (API returns 10 data at a time).
However in the view it can show only 4 - 5 cells as I set the row height to 175 points manually. Does anyone know why is this happening?
If this is how the tableView works what can I do to make my tableView to load only 10 initially?
Following is my code.
class MainViewController: UIViewController {
var tableView: UITableView!
var photoViewmodel: PhotoViewModel!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Home"
initTableView()
photoViewmodel = PhotoViewModel()
photoViewmodel.loadNewPhotos {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
func initTableView() {
tableView = UITableView.init(frame: self.view.frame, style: .plain)
tableView.delegate = self
tableView.dataSource = self
tableView.rowHeight = 175
tableView.separatorStyle = .none
tableView.register(HomeTableViewCell.nib, forCellReuseIdentifier: HomeTableViewCell.identifier)
view.addSubview(tableView)
}
}
extension MainViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return photoViewmodel.photos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("cellForRowAt")
let cell = tableView.dequeueReusableCell(withIdentifier: HomeTableViewCell.identifier, for: indexPath) as! HomeTableViewCell
cell.tag = indexPath.row
cell.configureCell(url: photoViewmodel.photos[indexPath.row].regularImgUrl, cacheKey: indexPath.row)
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
print("willDisplay")
let lastCell = photoViewmodel.photos.count - 1
if indexPath.row == lastCell {
print("add 10 more photos")
photoViewmodel.loadNewPhotos(
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
}
}

How to load Custom cell (Xib) in UITableview using Swift

Here's my code
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell!
bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
//NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell
cell = blucell
devname.text = peri[indexPath.row]
devstrength.text = signalstrength[indexPath.row]
bluetoothlog.backgroundColor = UIColor.clearColor()
return cell
}
I have tried with the above code and nothing is displayed in Tableview, please help me to change this code working
Thanks
Register your NIB with the reuse identifier:
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "Cell")
}
Your cellForRowAtIndexPath would then instantiate the cell.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! devicedet
return cell
}
the command registerNib should be called in ViewDidLoad, but not in each call of cell for row
what is 'bluecell'? In your call-back cellforTable.., you assign cell = blucell -> this can cause your problems.
Try this:
import UIKit
class YourViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//call register nib here!!
bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return peri.count
}
//it seem that you have to add a è
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
{
let cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell!
/* REMOVE THIS
bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
//NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell
*/
//// THEN, UPDATE YOUR CELL with YOUR DATAs
/* I don't understand what are you doing with this line of code:
cell = blucell
*/
devname.text = peri[indexPath.row]
devstrength.text = signalstrength[indexPath.row]
//Seem that bluetoothlog is the tableview, you should call it in viewdidload or viewwillappear()
bluetoothlog.backgroundColor = UIColor.clearColor()
return cell
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell :devicedet = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell!
bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
//NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell
cell = blucell
devname.text = peri[indexPath.row]
devstrength.text = signalstrength[indexPath.row]
bluetoothlog.backgroundColor = UIColor.clearColor()
return cell
}
In viewDidLoad register XIB as shown below
var userDetailsNIB = UINib(nibName: "UserDetailsCell", bundle: nil)
viewListingTable.registerNib(userDetailsNIB, forCellReuseIdentifier: "UserDetailsViewCellID")
And in func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
function use it by access it with Identifier which created in viewDidLoad method
let cell = tableView.dequeueReusableCellWithIdentifier("UserDetailsViewCellID") as UserDetailsViewCell
return cell
if you would like to Load custom cell(Xib) in UITableView in swift ,you may use the following code.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count; //This function returns number of rows in table view
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:YourTableViewCell! = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell", forIndexPath:indexPath)as! YourTableViewCell
// Do additional code here
}
Don’t forget to register nib in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// registering your nib
let yourNibName = UINib(nibName: "YourTableViewCell", bundle: nil)
tableView.registerNib(yourNibName, forCellReuseIdentifier: "YourTableViewCell")
// Do any additional setup after loading the view.
}
Register xib Cell in Swift
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "XibCell", bundle: nil), forCellReuseIdentifier: "Cell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell
return cell
}
My code:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(
UINib(nibName: "XibCell", bundle: nil),
forCellReuseIdentifier: "Cell"
)
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
return cell as! UITableViewCell
}
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//MARK:- Sample Data Array to load in TableView
let sampleArrray: \[String\] = \["val1", "val2", "val3", "val4", "val5"]
//MARK:- Cell reuse identifier
let cellReuseIdentifier = "cell"
//MARK:- UITableView outlet
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Registering the custom cell
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
// Setting delegate and Datasourse as same viewcontroller (this code is not neccessory if we are setting it from storyboard)
tableView.delegate = self
tableView.dataSource = self
}
//UITableview required methods
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sampleArrray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell!
cell.textLabel?.text = sampleArrray\[indexPath.row\]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("clicked at: \(indexPath.row)")
//Here u can get the selected cell and add action related to this cell seelction
}
Added Screenshot for setting Datasource and delegate from storyboard
Your fuction registerNib , you try to write in viewDidLoad() method and remove from cellForRowAtIndexPah and then test your project
Your Cell Instance should be of CustomCell instance and not of UITableView Cell.
var cell :UITableViewCell should be replaced by
var cell :devicedet

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

Resources