Add subview of xib tableview cell (swift) - ios

I have created a tableview and xib tableviewcell. i use the xib tableviewcell by adding subview. however it seems lost the function of func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
i want this custom tableviewcell inherit the normal cell in my tableview.
how to do that??
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var myTableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
myTableView.dataSource = self
myTableView.delegate = self
myTableView.backgroundColor = UIColor.whiteColor()
myTableView.frame = CGRectMake(20, 50, 250, 400)//Optional for table size
myTableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "myIdentifier")
self.view.addSubview(myTableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier") as! MyTableViewCell
myCell.myString1.text = "Row \(indexPath.row)"
myCell.myString2.text = "String \(indexPath.row)"
return myCell
}
}

You'll want to inherit from UITableViewController, and not UIViewController. As such:
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableview.delegate = self
self.tableview.backgroundColor = UIColor.whiteColor()
self.tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: nil), forCellReuseIdentifier: "myIdentifier")
)

Related

TableView inside Form Sheet not showing properly Swift

I'm trying to show a TableView inside a Form Sheet. This is totally working as expected on iPhone. However, when I test it on iPad for some reason the view is bigger than the Form Sheet.
Here is an Image of what I'm talking about:
iPad
As you can see the text isn't all visible in the frame. Full text is "THIS IS NOT SHOWING FULLY"
Here is what it is supposed to look like (ignore the dark mode)
iPhone
In MainController() I presented this page IntroController() by having this in ViewDidLoad()
let navController = UINavigationController(rootViewController: IntroController())
present(navController, animated: true, completion: nil)
In IntroController() this is the code.
import UIKit
var tableview: UITableView!
class IntroController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TableCell.reuseIdentifier()) as! TableCell
cell.label.text = "Amount"
cell.typeLabel.text = "THIS IS NOT SHOWING FULLY"
cell.selectionStyle = .none
cell.accessoryType = .none
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func viewDidLoad() {
super.viewDidLoad()
tableview = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), style: .grouped)
view.addSubview(tableview)
tableview.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
tableview.delegate = self
tableview.dataSource = self
}
}
Does anyone know why is this happening and how to fix it?
Thank you!
Instead of adding the table view to the view of VC as a subview, set it directly as the view:
view = tableview
Alternatively, use a UITableViewController directly. I strongly recommend you choose this approach.
class IntroController: UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
}
}
I am not sure what causes this, but if I were to guess, it is probably because that view.frame is, for some reason, not set to the rect of the content size of VC (yet) when viewDidLoad is called. If you move the code that adds the table view as a subview of view to viewDidLayoutSubviews, then it works as well:
override func viewDidLayoutSubviews() {
if view.subviews.count < 1 {
tableview = UITableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height), style: .grouped)
tableview.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
view.addSubview(tableview)
tableview.delegate = self
tableview.dataSource = self
}
}

Adding objects to custom UITableViewCell

Trying to use a custom cell which a label and two buttons.
I managed to create a subclass for custom cell and I need to show them in the cell as well. This is my code:
import UIKit
import Alamofire
import SwiftyJSON
class TableViewController: UITableViewController {
#IBOutlet var table: UITableView!
var fruits = ["Orange", "Apple", "Banana"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
tableView.rowHeight = 80
self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell")
}
#objc func downloadPressed() {
print("Download Button Presses")
}
#objc func previewPressed() {
print("Preview Button Pressed")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fruits.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! tableDetailsVCTableViewCell
cell.lable?.text = fruits[indexPath.row]
cell.dlB?.addTarget(self, action: #selector(downloadPressed), for: .touchUpInside)
cell.pvB?.addTarget(self, action: #selector(previewPressed), for: .touchUpInside)
return cell
}
}
I run the app but nothing shows up. No text in the label nor any buttons
As you said, you're using storyboard for designing the Custom TableViewCell then no need to register cell, just remove this line of code from viewDidLoad:
self.tableView.register(tableDetailsVCTableViewCell.self, forCellReuseIdentifier: "cell")
viewDidLoad looks like this:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
tableView.rowHeight = 80
}

Separators in custom UITableView are not shown

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()
}
}

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

Swift TableView for iOS

I have referred to this question, and tried to implement the various answers. I'm doing something dumb, as cellForRow is not being called, and I'm just getting a blank tableView. Are there other data source or delegate methods that I must override?
Thanks
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//tableView.delegate = self
//tableView.dataSource = self
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
//tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
println("cellForRowAtIndexPath")
println()
//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell!.textLabel.text = "Text Label"
cell!.detailTextLabel.text = "Detail Text Label"
return cell
}
}
UPDATE - The following appears to work.. Thanks for the help!!
class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//tableView.delegate = self
//tableView.dataSource = self
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
//tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
println("cellForRowAtIndexPath")
println()
//variable type is inferred
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell!.textLabel.text = "Text Label"
cell!.detailTextLabel.text = "Detail Text Label"
return cell
}
}
Here is working code. No need to register cell in -viewDidLoad(). Also UITableView is added as subview in Storyboard.
If someone has this kind of requirement.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
cell?.textLabel?.text = NSString(format: "%d", indexPath.row) as String
return cell!
}
}
You need to connect Delegate DataSource of TableView, both are connected with the tableview then can only NumberOfRowsInSection and CellForRowAtIndexpath called.

Resources