UITableView Using Swift

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

UITableViewController - iOS & Swift Tutorial, Overview. Step 1: Create an Xcode project & setup TableView: Open your Xcode -> create a new Xcode project -> Choose ios and single view application as your project template -> name it as you want -> create. Step 2: Setup TableView cell: Now drag and drop a TableView cell from the library to your table. iOS UITableView 100% Programmatically in Swift 4.0 STEP #1: Create a New Xcode Project. Create a project by opening Xcode and choose File → New → Project. Then, choose a STEP #2: Get Rid of Storyboard. Delete Main.Storyboard. Get rid of its reference as well by going to the top level of STEP

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

UITableView, This guide is made for beginners to learn the foundations of the UITableView class programmatically with auto layout in Swift. Use Swift as the project and Universal as the device. For good programming practices, we’ll be adding some autolayout to this app. Save the application. Add a Launch Screen. One good practice is to add something to the launchScreen.storyboard. Select the launchScreen.storyboard in the file inspector. Change the background to a color.

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.

Getting Started with UITableView in Swift, The example below is an adaptation and simplification of a longer post from We ❤ Swift. This is what it will look like: enter image description here. Create a New � Drag a UITableView onto your View Controller. Use auto layout to pin the four sides. Hook up the Outlets. Control drag from the Table View in IB to the tableView outlet in the code. Finished. That's all. You should be able run your app now. This answer was tested with Xcode 9 and Swift 4

UITableView tutorial in Swift, In this tutorial you will learn all the basics you need to know. Hint: This post is using iOS 10, Xcode 8.2.1 and Swift 3. On the one hand� Each subcategory has a UICollectionView and shows the items horizontally. Every item (UICollectionViewCell) has a UIView to show the color and a UILabel for the name of the color.

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
}

UITableView example for Swift, Implementing “tableView(_:numberOfRowsInSection:)”. A similar function is tableView(_:numberOfRowsInSection:) . Instead of providing the� view raw TableViewSwipeToDelete.swift hosted with ❤ by GitHub In this example, we filled we data an array named Items, and we removing the chosen item from the list when the user swipes left and press the delete button (or just swipes left all the way through).

UITableView - Tutorial For Beginners, To use a UITableView, you have to create objects that will serve as the DataSource and Delegate to it. The DataSource does what it says, it is� UITableView manages the basic appearance of the table, but your app provides the cells (UITableViewCell objects) that display the actual content. The standard cell configurations display a simple combination of text and images, but you can define custom cells that display any content you want.

Table View Controllers in Swift – LearnAppMaking, you a step by step tutorial on how to setup a TableView programmatically as well as using the delegate and datasource protocols in Swift 4.2. Using YLEXtensions could get you out of massive boilerplate code when you want to register, configure a UITableView or UICollectionView page which contains multiple types of cells.

Getting Started With UITableView in Swift, To connect the views to the MealTableViewCell.swift code In your storyboard, select the label in the table view cell. Click the Assistant button in the Xcode toolbar to open the assistant editor. If you want more space to work, collapse the project navigator and utility area by clicking the Navigator and Utilities buttons in the Xcode toolbar.

Comments
  • Are you sure that this line is correct? self.tableView.registerClass(TapCell1.self, forCellReuseIdentifier: "Cell") try to remove .self
  • TapCell1.self is equivalent to TapCell1.class in Objective-C.
  • Okay I will check @eXhausted
  • Are you using a atoryboard or a nib?
  • @MoorthyTheBoss: it's*
  • The correct answer comes from @Micah: you must add tableView:heightForRowAtIndexPath: in your code, it's an Xcode 6 (as of yet, beta6) bug. Nothing else is relevant to the question at hand.
  • should it be 0...70 (three dots not two)?
  • Swift 1.2 in Xcode 6.3 suggests that the as needs to be as!
  • Yes I know. Its already worked for me.But I need to display TapCell1
  • Now its displaying TapCell1 only. what else you want
  • TapCell1 colour is green. Finally i did. Thanks for your Valuable time.
  • This fixed my problem - but I'm confused, I set this up in Storyboard correctly, so why do i need to manually register in code also? This is different than objc. Any ideas?
  • @g_pass if you are using prototype cell in the storyboard no need to register it again by code. if you are using a custom cell only you could register it either by registerCell or registerNib
  • This helped me after 1 hour of pain. I thought the problem was swift related because I am just starting out in swift. But this shows I should have been thinking out of the box and done some basic checks first. Thanks.
  • -1, its old method, you should use tableView.registerNib(,forCellReuseIdentifier:) in viewDidLoad