My code:
//Adding Destination VC as subview in my current view's view container
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewControllerID")
containerView.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
//Now implementing Table view in the destination vc
class DestinationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
var tableData = ["Beach", "Clubs", "Chill", "Dance"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let screenBounds = UIScreen.main.bounds
let rect = CGRect(x: 0, y: 0, width: screenBounds.width, height: screenBounds.height)
tableView = UITableView(frame: rect, style: UITableViewStyle.plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.blue
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "my")
view.addSubview(tableView)
tableView.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath)
cell.textLabel?.text = "This is row \(tableData[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4//tableData.count
}
}
If I'm Setting the destination viewController as my initial vc from storyboard then everything is working fine.But if adding as subview in container view the cellForRowAt method is not being called but numberOfSection and numberOfRowsInSection is being called.
Is there anyone having any solution ?
Add self.addChildViewController(viewController) before subviewing
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewControllerID")
self.addChildViewController(viewController)
containerView.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
Make sure that constraints are set on UITableView. If so, try to to set height of UITableViewCell manually.
Firstly, you need to have a tableview in your storyboard. After creating tableView you may subview it programmatically by:
self.view.addSubview(tableView)
or add tableView in your storyboard and create an IBOutlet like this:
#IBOutlet weak var tableView: UITableView!
Secondly, you haven't initialized your cell, you have only dequeued a previously created cell(which, in your case has never got created). Your code should be something like this:
var cell = tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath) as UITableViewCell?
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "my")
}
Check your table's top-bottom-left-right constant. If your table's height is 0 then cellForRow method may not be call. If you not set your table's constant properly then then it's height may become 0.
Recheck are registered reusableidentifier
tableView.dequeueReusableCell(withIdentifier: "my", for: indexPath)
Related
I'm testing a simple tableView in a UIViewController for fun
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup() {
tableView.dataSource = self
tableView.delegate = self
tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomTableViewCell")
}
var data = [1,2,3,4,5,6,7]
}
extension ViewController : UITableViewDelegate {
}
extension ViewController : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath)
cell.textLabel?.text = data[indexPath.row].description
return cell
}
}
and I want to write a test to check that the correct data is being displayed in a presented cell.
My test looks like the following:
var controller: ViewController?
override func setUp() {
controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as? ViewController
}
func testViewCell() {
guard let controller = controller else {
return XCTFail("Could not instantiate ViewController")
}
let tableCell = Bundle(for: CustomTableViewCell.self).loadNibNamed("CustomTableViewCell", owner: nil)?.first as! CustomTableViewCell
tableCell.textLabel?.text = "2"
controller.loadViewIfNeeded()
let actualCell = controller.tableView!.cellForRow(at: IndexPath(row: 0, section: 0) )
XCTAssertEqual(actualCell, tableCell)
}
But the actual cell is nil. How can I test the presented cell in my view controller against an expected cell?
In your case I believe you will need to call reloadData on the table view as well. Try:
func testViewCell() {
guard let controller = controller else {
return XCTFail("Could not instantiate ViewController")
}
let tableCell = Bundle(for: CustomTableViewCell.self).loadNibNamed("CustomTableViewCell", owner: nil)?.first as! CustomTableViewCell
tableCell.textLabel?.text = "2"
controller.loadViewIfNeeded()
controller.tableView!.reloadData()
let actualCell = controller.tableView!.cellForRow(at: IndexPath(row: 0, section: 0) )
XCTAssertEqual(actualCell, tableCell)
}
In general for these cases I would also be worried about the view controller size. Since this is not put to any window it might in some cases use some intrinsic size and if that is for instance set to 0 your cells will not be there either. Maybe you should consider creating a window with fixed size (the size you want to test on) and apply your view controller as a root to it.
Also what do you expect to get from XCTAssertEqual(actualCell, tableCell)? Not sure but I would say this tests only pointers and will always fail. You will need to implement your own logic to check equality.
I am trying to have two UITableViews in the same ViewController. I am trying to do all of it programmatically but for some reason neither of the tableViews display at all.
I have noticed that the code never gets to
else if tableView == self.tableView2 {
}
in the cellForRowAt method. I have no idea why this is the case.
I appreciate any help to solve this.
import UIKit
class TestViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView1: UITableView?
var tableView2: UITableView?
let tableView1Data = ["Option 1", "Option 2", "Option 3", "Option 4", "Other"]
let tableView2Data = ["Cancel"]
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .apricot
// Initalize
tableView1 = UITableView()
tableView2 = UITableView()
// Register cells
// tableView1!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell1")
// tableView2!.register(UITableViewCell.self, forCellReuseIdentifier: "Cell2")
tableView1!.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "Cell1")
tableView2!.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "Cell2")
// Set delegates
tableView1!.delegate = self
tableView1!.dataSource = self
tableView2!.delegate = self
tableView2!.dataSource = self
// Add to view
view.addSubview(tableView1!)
view.addSubview(tableView2!)
// Set size constraints
tableView1!.translatesAutoresizingMaskIntoConstraints = false
tableView1!.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
tableView1!.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView1!.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
tableView2!.translatesAutoresizingMaskIntoConstraints = false
tableView2!.topAnchor.constraint(equalTo: tableView1!.bottomAnchor, constant: 15).isActive = true
tableView2!.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
tableView2!.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
// Customize looks
tableView1!.layer.cornerRadius = 10
tableView2!.layer.cornerRadius = 10
// Reload data
tableView1!.reloadData()
tableView2!.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
tableView1!.reloadData()
tableView2!.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView1 {
return tableView1Data.count
}
else if tableView == self.tableView2 {
return tableView2Data.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell?
if tableView == self.tableView1 {
cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
guard let cell = cell else {return UITableViewCell()}
let cellLabel = UILabel()
cellLabel.text = tableView1Data[indexPath.row]
cellLabel.textColor = .black
cell.addSubview(cellLabel)
cellLabel.frame = cell.frame
}
else if tableView == self.tableView2 {
cell = tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)
guard let cell = cell else {return UITableViewCell()}
let cellLabel = UILabel()
cellLabel.text = tableView2Data[indexPath.row]
cellLabel.textColor = .black
cell.addSubview(cellLabel)
cellLabel.frame = cell.frame
}
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return view.frame.height * 0.1
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//...
}
}
You need to somehow constrain the height of either tableView1 or tableView2 (Once you have established 1 height, the other can be inferred). At the moment you have told autolayout that the two table views must be 15 apart vertically, but not where that separation is in terms of the superview. As a result, autolayout is probably sizing tableView1 to the full height of the superview and the other tableview isn’t on screen, so it’s datasource methods don’t get called
For example, to set the table views to half the screen each:
TableView1!.bottomAnchor.constraint(equalTo:self.view.centerYAnchor, constant:-7).isActive=true
one caveat with tableView is that it's datasource method will not get called unless view has it's frame . If tableView try to render it self and doesn't find it's frame valid data source method will never get's called.
You need to check if both of the tableView has it's frame defined
I have a basic view controller with the following code:
class List2ViewController: UIViewController {
#IBOutlet weak var segmentedControl: UISegmentedControl!
#IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
segmentedControl.addTarget(self, action: #selector(showDummyViewController), for: UIControlEvents.valueChanged)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func showDummyViewController(){
let controller = storyboard?.instantiateViewController(withIdentifier: "tableController")
if let controller = controller {
controller.view.frame = view.bounds
view.addSubview(controller.view)
addChildViewController(controller)
controller.didMove(toParentViewController: self)
}
}
}
As you can see I am adding a child view controller that happens to be table view controller. The code for this table view controller is as follows:
class MyTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: CustomCellTypeIdentifiers.NoResultCell, bundle: nil)
tableView.register(nib, forCellReuseIdentifier: CustomCellTypeIdentifiers.NoResultCell)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.NoResultCell, for: indexPath) as! CellType
return cell
}
}
My problem is that when my table view is added as a child controller, it is not responsive to any touch inputs. The table view shows up but it does not scroll through or select any rows. Additionally, my cells do not show up in rows. Can you please explain to what I am missing in this implementation? Thank you in advance.
Heres a picture of the result:
One issue that i see here:
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.NoResultCell, for: indexPath)
should be like this:
let cell = tableView.dequeueReusableCell(withIdentifier: CustomCellTypeIdentifiers.NoResultCell, for: indexPath) as? CustomTableViewCellName
where CustomTableViewCellName should be replaced by the value of CustomCellTypeIdentifiers.NoResultCell i.e. your custom cell name, as i.e. your nibName also:
let nib = UINib(nibName: CustomCellTypeIdentifiers.NoResultCell, bundle: nil)
My haphazard attempt isn't calling cellForRowAtIndexPath . I'm assuming my setup is not right.
let wordTableView = WordTableView(frame: CGRectZero)
wordTableView.delegate = self
wordTableView.dataSource = self
wordTableView.words = words
let currentCell = wordTableView.cellForRowAtIndexPath(indexPath)
cell.contentView.addSubview(wordTableView)
wordTableView.viewDidLoad()
wordTableView.reloadData()
When I run this, my UITableView's viewDidLoad() get's called. But nothing actually loads. If I look at the Debug View Hierarchy, I can see that nothing has been added. So I assume I'm missing something particular about instantiating this tableView.
For reference, this is my UITableView. But to be honest, I'm completely guessing about what exactly a UITableView would need to get instantiated. And this would be my best attempt :
class WordTableView: UITableView {
var words = [Word]()
func viewDidLoad() {
self.registerNib(UINib(nibName: "WordCell", bundle: nil), forCellReuseIdentifier: "wordCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return words.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("WordCell", forIndexPath: indexPath) as! WordCell
let word = words[indexPath.row]
cell.sanskrit.text = "Sanskrit" //word.valueForKey("sanskrit")
cell.definition.text = "Definition" // word.valueForKey("definition")
return cell
}
}
You set wordTableView data source and delegate to the setter class, so table view data source and delegate function will not be called in WordTableView class.
Since you said your viewDidLoad is called, let set the delegate inside this function.
let wordTableView = WordTableView(frame: CGRectZero)
wordTableView.words = words
let currentCell = wordTableView.cellForRowAtIndexPath(indexPath)
cell.contentView.addSubview(wordTableView)
wordTableView.viewDidLoad()
wordTableView.reloadData()
And WordTableView class
class WordTableView: UITableView, UITableViewDelegate, UITableViewDataSource {
var words = [Word]()
func viewDidLoad() {
self.registerNib(UINib(nibName: "WordCell", bundle: nil), forCellReuseIdentifier: "wordCell")
self.delegate = self
self.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return words.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("WordCell", forIndexPath: indexPath) as! WordCell
let word = words[indexPath.row]
cell.sanskrit.text = "Sanskrit" //word.valueForKey("sanskrit")
cell.definition.text = "Definition" // word.valueForKey("definition")
return cell
}
}
As a side note, you should not call viewDidLoad() directly and it is better if you make WordTableView inherit UIViewController and put your table view inside it.
I think your viewDidLoad is being called because you are doing so explicitly, but your frame is CGRectZero, so it technically has no size, so there is nothing to add as a subView, so nothing gets called.
I think a better way to initialise the table view is to create your own initialiser, e.g.
class WordTableView: UITableView {
var words = [Word]()
init(frame: CGRect, wordArray: [Word], delegate: UITableViewDelegate, dataSource: UITableViewDataSource) {
self.frame = frame
words = wordArray
self.delegate = delegate
self.dataSource = dataSource
self.reloadData()
}
func viewDidLoad() {
self.registerNib(UINib(nibName: "WordCell", bundle: nil), forCellReuseIdentifier: "wordCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return words.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("WordCell", forIndexPath: indexPath) as! WordCell
let word = words[indexPath.row]
cell.sanskrit.text = "Sanskrit" //word.valueForKey("sanskrit")
cell.definition.text = "Definition" // word.valueForKey("definition")
return cell
}
}
And then in your setup:
let wordTableView = WordTableView(frame: cell.contentView.bounds, wordArray: words, delegate: self, dataSource: self)
let currentCell = wordTableView.cellForRowAtIndexPath(indexPath)
cell.contentView.addSubview(wordTableView)
cell.setNeedsDisplay()
I'm working with Swift 2.0 and Xcode 7.2.
I want to learn how to make an app without a storyboard (UI with pure programming code). To start off, I am trying to make a simple app, with three labels, inside a custom UITableView cell which will be updated dynamically through the internet.
Here is what I have achieved so far:
Created a new Swift project and deleted the main.storyboard from the project
Added a view controller as the rootViewController in AppDelegate
Included code to create a UITableView inside this view
Here are the other tasks I want to accomplish (all programmatically, without using the attribute inspector):
Insert a UINavigationController into the ViewController
Add a custom cell with three labels
Update the table view with data
If possible, I would want to have the ability to have everything working in landscape mode as well.
Can anyone tell me how to do this?
AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.backgroundColor = UIColor.whiteColor()
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
tableView.frame = CGRectMake(0 , 0, self.view.bounds.width, self.view.bounds.height)//Optional for table size
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myIdentifier")
myCell.textLabel?.text = "\(indexPath.row)"
myCell.detailTextLabel?.text = "Subtitle"
return myCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have no idea how to create a custom cell programmatically to which I can add objects.
Help would be appreciated.
Thanks.
If you are not using storyboard, you can define your cell just above the class where your ViewController where your are including your tableView something like myCell which is your custom UITableViewCell as given below.
In this myCell, you can add as many objects as your want and set them up in the setUpCell() block.
The full code is as below, please make sure you call setUpCell() when you use your cell's in cellForRowAtIndexPath.
ViewController.swift
import #UIKit
class myCell: UITableViewCell {
// Define label, textField etc
var aMap: UILabel!
// Setup your objects
func setUpCell() {
aMap = UILabel(frame: CGRectMake(0, 0, 200, 50))
self.contentView.addSubview(aMap)
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
// for ex, lets say, your data array is defined in the variable below
var dataArray = [[String:AnyObject]]() //Array of your data to be displayed
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
// register your class with cell identifier
self.tableView.registerClass(myCell.self as AnyClass, forCellReuseIdentifier: "Cell")
self.view.addSubview(tableView)
dataArray = // Something loaded from internet
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return flightDataArr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)
var cell:myCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? myCell
if cell == nil {
cell = myCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
var data = dataArray[indexPath.row]
cell?.setUpCell()
cell!.aMap.text = String(dict["productName"])
return cell!
}
}
See if this works for you. I never used programming to create tableView, so this may not be the optimal way to create your tableView programmatically. I hope someone else may help you with a better answer if possible.
You can create a sub class of UITableViewCell say PackageListTableViewCell.
Declare number of labels in tabelViewCell custom class as per your requirements like below,
var label1 : UILabel?;
override init:reuseIdentifier: in custom cell with additional parameters as below.
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//create labels as per your requirement
self.label1 = //initialise you label
//set frame, or constraint
//set text color, background color etc
//add created labels to cell as below
self.contentView.addSubView(self.label1);
}
your tableView:cellForRowAtIndexPath: will be look like,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let lable1String = "lbl1"
let lable2String = "lbl2"
let lable3String = "lbl3"
var cell : PackageListTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellID") as?PackageListTableViewCell
if (cell == nil) {
cell = PackageListTableViewCell.init(style: UITableViewCellStyle.Default,
reuseIdentifier:"cellID");
}
cell.selectionStyle = UITableViewCellSelectionStyle.None;
//set text of your lables as below
cell.label1.text = lable1String;
return cell;
}
You have to register a custom tableviewcell class using method registerClass on tableview.
Use this modified Viewcontroller code:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myIdentifier")
tableView.frame = CGRectMake(0 , 0, self.view.bounds.width, self.view.bounds.height)//Optional for table size
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)
myCell.textLabel?.text = "\(indexPath.row)"
myCell.detailTextLabel?.text = "Subtitle"
return myCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}