class TableController: UIViewController {
#IBOutlet var ListTable: UITableView!
var list: [Dictionary<String, String>] = []
override func viewDidLoad() {
super.viewDidLoad()
let ListTable = UITableView(frame: view.bounds)
self.ListTable = ListTable
ListTable.dataSource = self
ListTable.delegate = self
initList()
}
func initList() {
// get list from firebase
self.ListTable.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = self.list[indexPath.row]
let cellIdentifier = "ListCell"
let cell = CustomCell(style: .default, reuseIdentifier: cellIdentifier)
cell.foodLabel?.text = item["Banana"]
return cell
}
}
extension QueueController: UITableViewDataSource, UITableViewDelegate {
}
CustomCell class:
import UIKit
class CustomCell: UITableViewCell
{
#IBOutlet weak var foodLabel: UILabel!
override func awakeFromNib()
{
super.awakeFromNib()
}
}
My data from firebase loads properly. On storyboard I have a normal view controller with a UITableView embedded inside of it. That table view is liked to my IBOutlet for my ListTable. In the table there is a cell with 3 labels. That cell has the identifier ListCell and it's class is CustomCell.
Edit: There is no error but my data isn't showing up.
This is because your Custom Cell does not dequeue properly. Try this one
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "ListCell"
var cell : ListCell? = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! ListCell?
if (cell == nil) {
cell = Bundle.main.loadNibNamed("ListCell", owner: nil, options: nil)?[0] as? ListCell
}
cell?.backgroundColor = UIColor.clear
cell?.contentView.backgroundColor = UIColor.clear
return cell!
}
Perhaps try registering your cell in viewDidLoad
ListTable.register(UINib(nibName: "CustomCell", bundle: Bundle.main), forCellReuseIdentifier: "ListCell") //this is assuming that your nib is named "CustomCell"
Also, for the record, you should follow camel-case conventions and name your UITableView listTable
You did never add the TableView to your view... (or part go the code is missing )
Related
Collectionview reload not working when reload from custom tablecell.
I have put a dropdown inside one collectionview cell.this dropdown also not working.
Here is the code:
class HotelAvailableRoomsTVCell: UITableViewCell {
#IBOutlet weak var collectionView: UICollectionView!
//MARK: Life cycle
override func awakeFromNib() {
super.awakeFromNib()
let nib = UINib.init(nibName: "AvailableRoomCVCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "AvailableRoomCVCell")
collectionView.delegate = self
collectionView.dataSource = self
// Initialization code
}
func initCellWithData(roomJSON:JSON,indexPath:IndexPath,isSingle:Bool) {
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
}
In viewController:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "HotelAvailableRoomsTVCell", for: indexPath) as! HotelAvailableRoomsTVCell
cell.delegate = self
cell.initCellWithData(roomJSON: (json)!, indexPath: indexPath as IndexPath, isSingle: true)
return cell;
}
Screen is:
I have used a tableview with dynamic number of cells as collectionview. this collection view contains a dropdown as shown in image
First of all, do this & check
class HotelAvailableRoomsTVCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource
You can also do this, (already done by me and its working),
Pass the data by creating var in HotelAvailableRoomsTVCell.
var collectionData : [String : Any] = []
In cellForRow
cell.collectionData = data
I am making a view controller with tableView for presenting individual news cards. I want to load cells once and for all (without reusing it as usual). So I do this:
tableView.register(UINib(nibName: "RatingNewsCell", bundle: nil), forCellReuseIdentifier: "RatingNewsCell")
tableView.register(UINib(nibName: "PromoNewsCell", bundle: nil), forCellReuseIdentifier: "PromoNewsCell")
var cells = [UITableViewCell]()
let rating = RatingNewsCell()
rating.delegate = self
cells.append(rating)
etc...
self.items = cells
Cells are loaded like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
return items[indexPath.section]
}
Cell code looks like this:
class RatingNewsCell: UITableViewCell
{
var delegate: RatingNewsCellDelegate?
#IBOutlet weak var shopNameLabel: UILabel!
#IBOutlet weak var bouquetImageView: RoundImageView!
#IBOutlet weak var floristImageView: RoundImageView!
#IBOutlet weak var ratingControl: RatingPicker!
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: "RatingNewsCell")
}
required init?(coder decoder: NSCoder)
{
super.init(coder: decoder)
}
override func awakeFromNib()
{
super.awakeFromNib()
ratingControl.setSelected(newIndex: 0)
}
}
In the result I've got a table view, but cell just appears blank:
Any ideas?
I suggest to refactor your code to use tableView's datasource method though you are only using static cells.
enum Section: Int {
case RatingNews
case PromoNews
static var count: Int { return Section.PromoNews.rawValue + 1 }
}
func numberOfSections(in tableView: UITableView) -> Int {
return Section.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let section = Section(rawValue: indexPath.section)!
switch section {
case .PromoNews:
let cell = tableView.dequeueReusableCell(withIdentifier: "RatingNewsCell", for: indexPath) as! RatingNewsCell
return cell
case .RatingNews:
let cell = tableView.dequeueReusableCell(withIdentifier: "PromoNewsCell", for: indexPath) as! PromoNewsCell
return cell
}
}
You need to call
tableView.delegate = self
and
tableView.dataSource = self
I'm lost. I searched and searched and cannot find the reason why my custom cell isn't displayed.
// ProfileCell.swift:
import UIKit
import QuartzCore
class ProfileCell: UITableViewCell {
#IBOutlet weak var profileNameLabel: UILabel!
#IBOutlet weak var profilePictureView: UIImageView!
}
The second default TableViewCell is displayed normally. I have no missing constraints in Interface Builder or any Errors. ProfileCell is selected in the ProfileCell.xib identity tab as the custom class.
// MoreTableViewControllerIB.swift
import UIKit
class MoreTableViewControllerIB: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// this cell is missing
if indexPath.row == 0 {
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as! ProfileCell
cell.profileCommentLabel.text = "Test Comment Label"
cell.profilePictureView.image = UIImage(named:"profile_picture_test")
return cell
// this cell is displayed perfectly
}else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell") ?? UITableViewCell(style: .default, reuseIdentifier: "statisticsCell")
cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
cell.textLabel?.text = "Statistics"
cell.imageView.image = UIImage(named:"statistics")
return cell
// has to return a cell in every scenario
}else{
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
return cell
}
}
}
Here is a screenshot of what I get.
tableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")
add this line in viewDidLoad or viewWillApppear
So I found out what my mistake was. Pretty stupid and it cost me half a day:
The cell was already displayed, but the default height wasn't big enough to see it. I thought the set height in the .xib would be used. It apparently is not.
So I added this:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 192 // the height for custom cell 0
}
}
In my case I had to additionally register nib with view of my cell:
myTableView.register(UINib(nibName: "nibwithcell", bundle: nil), forCellReuseIdentifier: "cell") // you need to register xib file
I have a UIViewController in which I've embedded UITableView. Because I don't want the UIViewController to get too heavy I separated the UITableViewDataSource and UITableViewDelegate
class ViewController: UIViewController {
#IBOutlet var tableView: UITableView!
var dataSource : UITableViewDataSource!
var tableDelegate: UITableViewDelegate!
override func viewDidLoad() {
super.viewDidLoad()
dataSource = TableViewDataSource()
tableDelegate = TableViewDelegate()
tableView.dataSource = dataSource
tableView.delegate = tableDelegate
// Do any additional setup after loading the view, typically from a nib.
}
}
class TableViewDataSource : NSObject, UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "MyCellIdentifier"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
cell.textLabel?.text = "hello"
cell.detailTextLabel?.text = "world"
return cell
}
}
class TableViewDelegate : NSObject, UITableViewDelegate {
//custom code here if required
}
In my Storyboard, I've created a prototype cell within the UITableView with the identifier
MyCellIdentifier
I use this identifier to create a cell in my UITableViewDataSource delegate method.
However, if I start the app, only the text of the left label is displayed. The detail label is not visible.
I looked into the debugger and noticed that detailLabel text is correct. The text of the right label is really "world". However, the label is not visible.
I've done a little bit of research and there has been a similar problem in the past. Whenever the text of the detailLabel was set to nil, it was not visible
However in my example, the text is not nil, it is set to "Detail":
How can I make the right label visible?
if (cell != nil)
{
cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,
reuseIdentifier: reuseIdentifier)
}
add this code under let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) this line of your code.
My TapCell1.swift
This is Custom UITableViewCell View
import UIKit
class TapCell1: UITableViewCell
{
#IBOutlet var labelText : UILabel
init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
println("Ente")
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}
override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
}
My ViewController.swift
Its All DataSource and Delegates are set correctly.But My custom Cell is not displaying.
import UIKit
class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
#IBOutlet var label: UILabel
#IBOutlet var tableView : UITableView
var getvalue = NSString()
override func viewDidLoad()
{
super.viewDidLoad()
label.text="HELLO GOLD"
println("hello : \(getvalue)")
self.tableView.registerClass(TapCell1.self, forCellReuseIdentifier: "Cell")
}
func tableView(tableView:UITableView!, numberOfRowsInSection section:Int)->Int
{
return 5
}
func numberOfSectionsInTableView(tableView:UITableView!)->Int
{
return 1
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1
cell.labelText.text="Cell Text"
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The Problem is My custom cell is Not displayed. Please suggest anything i did wrong.
Note: here is My code My File Download Link
I finally did it.
For TapCell1.swift
import UIKit
class TapCell1: UITableViewCell {
#IBOutlet var labelTitle: UILabel
init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}
}
For NextViewController.swift
import UIKit
class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: UITableView
var ListArray=NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
let nibName = UINib(nibName: "TapCell1", bundle:nil)
self.tableView.registerNib(nibName, forCellReuseIdentifier: "Cell")
for i in 0...70 {
ListArray .addObject("Content: \(i)")
}
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int)->Int {
return ListArray.count
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 51
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1
//cell.titleLabel.text = "\(ListArray.objectAtIndex(indexPath.item))"
cell.labelTitle.text = "\(ListArray.objectAtIndex(indexPath.row))"
return cell
}
}
My working code link: CUSTOMISED TABLE
You should register the class for the cell. For that do
change this line of code to
self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell")
Edit
You code is looks fine i checked it
//cell.labelText.text="Cell Text"
cell.textLabel.text="Cell Text" // use like this
The solution is most likely pinpointed to setting the cell height manually as such:
override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
return 44
}
I believe it's an Xcode beta 6 bug.
I have now been able to get Custom UITableViewCell to work.
Works on
Runs on Xcode 6 beta 6
Runs on Xcode 6 beta 5
iOS is 7.1
How
I have created a ".xib" file for the cell.
I copy it into the storyboard.
Via the storyboard I give it a Identifier.
I make sure it is a sub child of a tableview
Doing it this way, you do not need to register a class / nib etc.
This is my custom cell.
import UIKit
class TestCell: UITableViewCell {
#IBOutlet var titleImageView: UIImageView!
#IBOutlet var titleLabel: UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
In your view, or where ever you extend "UITableViewDataSource".
Make sure "cell2" is the same as the "Identifier" that you gave it via the storyboard.
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell:TestCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as TestCell
// Example of using the custom elements.
cell.titleLabel.text = self.items[indexPath.row]
var topImage = UIImage(named: "fv.png")
cell.titleImageView.image = topImage
return cell
}
uitableviewcell
Check your Story board select the cell and look at the "identity inspector in that select CLASS type your CustomClass and MODULE type your project name
I have done this It works perfectly try this tip to avoid error to see below image and code
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
// let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
// cell.textLabel.text = array[indexPath!.row] as String
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell
cell.nameLabel.text = array[indexPath!.row] as String
cell.RestaurentView.image = UIImage(named:images[indexPath!.row])
return cell
}
Try this following code
var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as CustomTableViewCell
https://github.com/iappvk/TableView-Swift
If you are not using Storyboard then create a new file as subclass of UITableViewCell check the checkbox "also create xib files" after that set your custom cell .and here is the code for tableview
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var customcell:CustomTableViewCellClass? = tableView.dequeueReusableCellWithIdentifier("cell") as? CustomTableViewCellClass
if (customcell==nil)
{
var nib:NSArray=NSBundle.mainBundle().loadNibNamed("CustomTableViewCellClass", owner: self, options: nil)
customcell = nib.objectAtIndex(0) as? CustomTableViewCell
}
return customcell!
}
Try this following code:
var cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellTableView") as? CustomCellTableView
Extention of NSOject
extension NSObject {
var name: String {
return String(describing: type(of: self))
}
class var name: String {
return String(describing: self)
}
}
Properties on Custom TableViewCell
class var nib: UINib {
return UINib(nibName:YourTableViewCell.name, bundle: nil)
}
class var idetifier: String {
return YourTableViewCell.name
}
Add in UIViewController
self.tableView.registerNib(YourTableViewCell.nib, forCellReuseIdentifier: YourTableViewCell.idetifier)
let cell = tableView.dequeueReusableCellWithIdentifier(YourTableViewCell.idetifier, forIndexPath: indexPath) as! YourTableViewCell
It is Purely swift notation an working for me
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cellIdentifier:String = "CustomFields"
var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell
if (cell == nil)
{
var nib:Array = NSBundle.mainBundle().loadNibNamed("CustomCell", owner: self, options: nil) cell = nib[0] as? CustomCell
}
return cell!
}