UITableViewDataSource not being invoked in Swift, Xcode 7 beta 2 - swift

This is a bizarre one, & I'm quite happy to be told that I've missed something obvious, but I can't see it.
Firstly, the UITableViewDataSource methods are not even showing up in autocomplete, then they're not called at runtime. Weirdly, the UITableViewDelegate methods are working on both counts.
Here's the (cut-down) code -
class PopupTable : NSObject, UITableViewDataSource, UITableViewDelegate
{
private var tableView : UITableView
init(rect: CGRect)
{
tableView = UITableView(frame: rect, style: UITableViewStyle.Plain)
super.init()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "PopupCell")
tableView.dataSource = self
tableView.delegate = self
}
func show()
{
if let mainWindow = UIApplication.sharedApplication().delegate?.window!
{
mainWindow.addSubview(tableView)
}
}
// MARK: TableView Delegate & DataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return myData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("PopupCell")!
cell.detailTextLabel?.text = "some text"
return cell
}
}
I have no problems getting this to work in a UIViewController, but that's not what I want here.

Related

I want to position top UIView on top of UITableView using SnapKit

I'm written in snapkit for UI render. I have not use storyboard or nib files.
I want to position top UIView on top of UITableView using SnapKit.
How to write it?
Below is my written code
swift
import UIKit
import SnapKit
class ViewController: UIViewController {
var data: [String] = []
var tableView = UITableView()
var subView = UIView()
var label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
label.text = "Hello?"
self.view.addSubview(self.tableView)
self.tableView.addSubview(self.subView)
self.subView.addSubview(self.label)
self.subView.backgroundColor = .gray
self.tableView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
self.subView.snp.makeConstraints { make in
make.top.width.equalTo(self.tableView)
make.centerX.equalTo(self.tableView)
make.height.equalTo(200)
}
self.label.snp.makeConstraints { make in
make.centerX.centerY.equalTo(self.subView)
}
for i in 0...100 {
data.append("\(i)")
}
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let element = self.data[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "comicListCell") ?? UITableViewCell(style: .normal, reuseIdentifier: "comicListCell")
cell.textLabel?.text = element
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(self.data[indexPath.row])
}
}
My code :
I want it!:
UIView must placed into UITableView children !!
Sorry my bad english..
Thanks for reading
You want subview which is sticky header of tableview. Change this line self.tableView.addSubview(self.subView) to
tableView.tableHeaderView = subView.
You could easily add the tableView method:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return self.subView
} else {
return UIView.init(frame: CGRect.zero)
}
}

Create custom UITableView class

How i can to create heir of UITableView class that will contain delegate and dataSource methods? I don't want to have dataSource and delegate methods in my ViewController.
You need to create another class for the purpose but first let's see how the ViewController will be.
As you can see the code is sort of self explanatory, I have created a custom class called TableViewDelegate that will be set as a delegate and dataSource of the tableView.
We are passing to TableViewDelegate, the data to be shown in the tableView, and function named didSelectRow that will be called by TableViewDelegate once a row is selected.
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
// data source
var data = [1, 2, 3, 4]
// delegate
var tableViewDelegate: TableViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
// creating the delegate object and passing the data
tableViewDelegate = TableViewDelegate(data: data)
// passing a function to the delegate object
tableViewDelegate?.didSelectRow = didSelectRow
// setting the delegate object to tableView
tableView.delegate = tableViewDelegate
tableView.dataSource = tableViewDelegate
}
// a function that will be called by the delegate object
// when a row is selected
func didSelectRow(dataItem: Int, cell: UITableViewCell) {
let alert = UIAlertController(title: "Info", message: "\(dataItem) was selected.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
}
The TableViewDelegate that is in charged of everything related of UITableViewDelegate, and UITableViewDataSource protocols.
class TableViewDelegate: NSObject, UITableViewDelegate, UITableViewDataSource {
var data = [Int]()
// variable that holds a stores a function
// which return Void but accept an Int and a UITableViewCell as arguments.
var didSelectRow: ((dataItem: Int, cell: UITableViewCell) -> Void)?
init(data: [Int]) {
self.data = data
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let text = String(data[indexPath.row])
cell.textLabel?.text = text
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)!
let dataItem = data[indexPath.row]
if let didSelectRow = didSelectRow {
// Calling didSelectRow that was set in ViewController.
didSelectRow(dataItem: dataItem, cell: cell)
}
}
}
Result:
I did this to avoid a long ViewController with a UIPickerViewDelegate/DS. You can simply make a class that conforms to UITableViewDelegate and UITableViewDataSource, instantiate this object in your view controller and assign it as the dataSource and delegate of the table view. For this class to send stuff back to your ViewController, you will have to make a protocol for the VC to conform to and give the class a delegate as well.
I read that this class must inherit from NSObject as the protocols are NSObject protocols, and it throws and error if they don't.
class MyCustomTableViewDel: NSObject, UITableViewDataSource, UITableViewDelegate {
weak var secondaryDelegate: TableViewSecondaryDelegate?
let rowData: [String]
init(dataForRows: [String]) {
rowData = dataForRows
super.init()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return rowData.count
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
secondaryDelegate?.doSomething(indexPath.row)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
....
return SomeCellForTheTableView
}
}
then make the secondary protocol:
protocol TableViewSecondaryDelegate {
func doSomething(row: Int)
}
then in your ViewController:
class myTableViewSceneController: UIViewController, TableViewSecondaryDelegate {
override func viewDidLoad() {
....
let tableViewDelAndDS = MyCustomTableViewDel(dataForRows: ["row0", "row1"])
tableViewDelAndDS.secondaryDelegate = self
tableView.delegate = tableViewDelAndDS
tableView.dataSource = tableViewDelAndDS
}
func doSomething(row: Int) { ... }
}

Can't resolve SIGABRT error - Times Table App with UITableView

I am trying to build a times tables app with a slider and a UITableView, but it gives me the SIGABRT error. I have tried relinking every outlet and action as well as redoing it from scratch, but it still won't seem to work.
Since I cannot paste it in here correctly formatted, I put it on pastebin.com
Debug code: http://pastebin.com/cBaALXWq
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBOutlet weak var sliderValue: UISlider!
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
#IBAction func sliderMoved(sender: AnyObject) {
var timesTableIndex: Int = Int(sender.value)!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel?.text = String(timesTableIndex*indexPath.row+1)
return cell
}
}
}
AppDelegate.swift; gives error in line 4
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
EDIT
#IBOutlet weak var sliderValue: UISlider!
var timesTableIndex: Int?
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
#IBAction func sliderMoved(sender: AnyObject) {
timesTableIndex = Int(sliderValue.value * 50)
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text = String(timesTableIndex*(1+indexPath.row))
return cell
}
I looks like you've added cellForRowAtIndex: inside #IBAction method. What you need to do is to take it out from the method. In addition you need set the timesTableIndex variable as global so that it can be accessed in cellForRowAtIndexPath: and call the reloadData method on tableView when you call sliderMoved: method.
var timesTableIndex: Int = 0 // Now global variable
#IBAction func sliderMoved(sender: AnyObject) {
timesTableIndex = Int(sender.value)!
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = "\(timeTableIndex * indexPath.row + 1)"
return cell
}
Also it seems that you are not using dequeueReusableCellWithIdentifier method on your table view, so I've updated the code to use it. Not that you need to set the Cell reuse identifier in your storyboard.
This should be your tableview code if you have created tableview in the storyboard
#IBOutlet weak var sliderValue: UISlider!
#IBOutlet weak var tableview: UITableView!
var timesTableIndex: Int = 0
#IBAction func sliderMoved(sender: AnyObject) {
timesTableIndex = Int(sliderValue.value * 50)
tableview.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timesTableIndex
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel!.text = String(1*(1+indexPath.row))
return cell
}
If you have created your cell in the storyboard set the CellReuseIdentifier as 'cell' in the storyboard else you can register the class in the like
override func viewDidLoad() {
super.viewDidLoad()
//tableView is IBOutlet instance of UITableview
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "test")
}
Also please check you have set the tableview delegate/datasource in the storyboard tableview object
I've managed to solve the problem. I accidentally put the function which sets up the prototype cell outside the ViewController class, that explains everything. Thank you for helping me!

Dynamic datasource/delegates for UITableView in swift

I need to set up different objects based on certain conditions as the datasource & delegate for table view.
But I am not able to assign tableview's datasource/delegate as it throws some errors.
Cannot assign a value of type NSObject? to a value of type UITableViewDelegate?
I did check this Q&A but this did not work.
var dataSourceDelegate:NSObject?
class RootViewController: UIViewController {
...
override func viewDidLoad() {
dataSourceDelegate = TableDataSourceDelegate()
// Table View
tableView = UITableView()
tableView!.setTranslatesAutoresizingMaskIntoConstraints(false)
tableView!.dataSource = dataSourceDelegate
// Cannot assign a value of type NSObject? to a value of type UITableViewDataSource?
tableView!.delegate = dataSourceDelegate
// Cannot assign a value of type NSObject? to a value of type UITableViewDelegate?
view.addSubview(tableView!)
// Constraints
var views:[String:UIView] = ["table":tableView!]
var hTableConstraint = "H:|[table]|"
var vConstraint = "V:|[table]|"
view.addConstraintsToView([hTableConstraint, vConstraint], view: view, viewVariables: views)
}
...
}
This is the datasource/delegate class
class TableDataSourceDelegate:NSObject, UITableViewDataSource, UITableViewDelegate {
// MARK: Datasource
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
// MARK: Delegates
}
NSObject? doesn't conforms to UITableViewDelegate, neither to UITableViewDataSource. You should create your protocol like
protocol GeneralDataSource: UITableViewDataSource, UITableViewDelegate {}
And then all data sources should conform that protocol.
class MyDataSource: NSObject, GeneralDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return UITableViewCell()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
}
Then you can use it like this
var myDataSource: GeneralDataSource?
override func viewDidLoad() {
super.viewDidLoad()
self.myDataSource = MyDataSource()
self.tableView.delegate = self.myDataSource
}
This is how your TableDataSourceDelegate should look like:
import UIKit
class TableDataSourceDelegate: NSObject {
}
extension TableDataSourceDelegate: UITableViewDataSource {
#objc func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 0
}
#objc func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "defaultCell")
cell.textLabel?.text = "test"
return cell
}
}
extension TableDataSourceDelegate: UITableViewDelegate {
// your delegate implementation here
}
And view controller implementation
import UIKit
// The typealias definition
typealias TVDataSourceDelegate = protocol<UITableViewDataSource, UITableViewDelegate>
class ViewController: UIViewController {
var dataSourceDelegate: TVDataSourceDelegate?
var tableView: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
dataSourceDelegate = TableDataSourceDelegate()
// Table View
tableView = UITableView()
tableView!.translatesAutoresizingMaskIntoConstraints = false
tableView!.dataSource = dataSourceDelegate
tableView!.delegate = dataSourceDelegate
view.addSubview(tableView!)
// other code ...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Although, I would recommend to separate dataSource and delegate objects (e.g. put the delegate protocol conforming code into your view controller's code.

Why can I not assign dataSource directly?

I would like to understand why to following Swift code does not work, but using the commented version does. I'm not sure if dataSources are typically wrapped into a separate class, but I don't think that should matter. I'm using Xcode 6.3.2, all up to date.
// MainViewController.swift
import UIKit
class MainViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
var dataSource:UITableViewDataSource?
override func viewDidLoad() {
super.viewDidLoad()
// dataSource = MainTableViewDataSource()
// tableView.dataSource = dataSource
tableView.dataSource = MainTableViewDataSource()
}
}
The MainTableViewDataSource is just a class which implements the UITableViewDataSource protocol and uses some dummy data.
// MainTableViewDataSource.swift
import UIKit
class MainTableViewDataSource : NSObject, UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 100
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1000
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return String(section + 1)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
cell.textLabel?.text = "Joejoe"
return cell
}
}
According to Apple's documentation https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instp/UITableView/dataSource
dataSource property of UITableView is unowned in Swift which is (assign) for Objective-C meaning that this property does not increase the reference count. So right after viewDidLoad function, when reference count of your MainTableViewDataSource becomes zero, it gets deallocated.
I recommend reading: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
You'll run into strange outcomes--sometimes even inconsistent--if you don't do the memory management right.