简体   繁体   中英

Creating a UITableViewCell programmatically in Swift

I am trying to create a custom cell for my UITableView but I am having some difficulty.

First off I cannot use the Interface Builder, as I am experiencing a variation on this bug in Xcode . Every time I click on an element in the Interface Builder everything in that view gets a height and width of zero and gets repositioned outside of the view. Besides, I would like to learn how to do this programmatically.

Secondly I am using the Swift language for my project. I have been trying to follow this demonstration , and doing my best to convert the Objective C code over to Swift, but whenever I run into problems I end up being stuck. I presume this is because I am not converting the code over correctly.

Thirdly I found this video but despite being fairly difficult to follow (lots of the code is just copied and pasted without much explanation to what it does or why), it still ends up using the Interface Builder to change various parts.

I have a basic UITableView set up fine. I just want to be able to add a custom cell to that table view.

  • Can this be done using pure programming, or do I need to use the Interface Builder?

  • Can anyone point me in the right direction or help me out in creating a custom cell programmatically in Swift?

Many thanks.

In general: Everything is possible in pure programming ;-)

  1. Create a custom class for your tableView cell and there setup all the elements, properties and the visual layout. Implement the required methods init(style,reuseidentifier)

  2. In your custom class for the UITableViewController register the custom cell class using registerClass(forCellReuseIdentifier)

  3. Setup your delegate and datasource for the custom tableViewController

Finally, you create the cells in cellForRowAtIndexPath :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("myReuseIdentifier", forIndexPath: indexPath) as MyCustomTableViewCell 

    // configure the cell using its properties

    return cell
}

This should be the basic steps.

If you're looking for more code, here is an example of a custom cell that I created:

//  File: vDataEntryCell.swift
import UIKit

class vDataEntryCell: UITableViewCell
{

//-----------------
// MARK: PROPERTIES
//-----------------

//Locals
var textField : UITextField = UITextField()

//-----------------
// MARK: VIEW FUNCTIONS
//-----------------

///------------
//Method: Init with Style
//Purpose:
//Notes: This will NOT get called unless you call "registerClass, forCellReuseIdentifier" on your tableview
///------------
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
    //First Call Super
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    //Initialize Text Field
    self.textField = UITextField(frame: CGRect(x: 119.00, y: 9, width: 216.00, height: 31.00));

    //Add TextField to SubView
    self.addSubview(self.textField)
}


///------------
//Method: Init with Coder
//Purpose:
//Notes: This function is apparently required; gets called by default if you don't call "registerClass, forCellReuseIdentifier" on your tableview
///------------
required init(coder aDecoder: NSCoder)
{
    //Just Call Super
    super.init(coder: aDecoder)
}
}

Then in my UITableViewController class I did the following:

//  File: vcESDEnterCityState.swift
import UIKit

class vcESDEnterCityState: UITableViewController
{

//-----------------
// MARK: VC FUNCTIONS
//-----------------


///------------
//Method: View Will Appear
//Purpose:
//Notes:
///------------
override func viewWillAppear(animated: Bool)
{
    //First Call Super
    super.viewWillAppear(animated)

    //Register the Custom DataCell
    tvCityStateForm.registerClass(vDataEntryCell.classForCoder(), forCellReuseIdentifier: "cell")
}

//-----------------
// MARK: UITABLEVIEW DELEGATES
//-----------------

///------------
//Method: Cell for Row at Index Path of TableView
//Purpose:
//Notes:
///------------
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    //Get Reference to Cell
    var cell : vDataEntryCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as vDataEntryCell

    //...Do Stuff

    //Return Cell
    return cell
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM