create a UITableViewController programmatically in Swift - uitableview

I'm trying to, as the title say, set up a UITableViewController programmatically. After a few hours of trying I hope someone can help me. And, yes, I hve checked out other posts on this matter:
import UIKit
class MainViewController: UITableViewController {
init(style: UITableViewStyle) {
super.init(style: style)
// Custom initialization
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
var cell = tableView?.dequeueReusableCellWithIdentifier("Cell") as? UITableViewCell
if !cell {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "Cell")
}
cell!.textLabel.text = "test"
return cell
}
}
and the appDelegate looks like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainViewController: UITableViewController = MainViewController(style: UITableViewStyle.Plain)
let navigationController: UINavigationController = UINavigationController()
navigationController.pushViewController(mainViewController, animated: false)
self.window!.rootViewController = navigationController
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
The program run, but as soon as it does, I get the following error:
fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'HelloWorld.MainViewController'
I then changes the MainViewController(style: UITableViewStyle.Plain) to MainViewController(nibName: nil, bundle: nil) but then I get the following syntax error: Extra argument 'bundle' in call
Any help would be much appreciated

I'm using a subclass of UITableViewController with no issues, using the (nibName:bundle:) form, but I've overridden that in my subclass. I tried replacing my subclass with a standard UITableViewController, and it still worked fine. Are you possibly overriding an init(...) method in your subclass?

In the AppDelegate or wherever you call the TableViewController:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
if let window = window {
window.backgroundColor = UIColor.whiteColor()
var mainTableViewController = MainTableTableViewController(style: .Plain)
window.rootViewController = UINavigationController(rootViewController: mainTableViewController)
window.makeKeyAndVisible()
}
return true
}
In the UITableViewController (assuming no custom TableViewCell implementation):
import UIKit
class MainTableTableViewController: UITableViewController {
override init(style: UITableViewStyle){
super.init(style: style)
}
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!){
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
// MARK: - Tableview Data Source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return [*YOUR_DATA_ARRAY].count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = [*YOUR_DATA_ARRAY][indexPath.row]
}
For an full example: visit: https://github.com/ericcgu/EGStormTracker

I just removing
init(style: UITableViewStyle) {
super.init(style: style)
}
and then init like this:
let mainViewController: UITableViewController = MainViewController()

Related

Navigation bar with searchBarController of UITabBarController flickering on changing the color of navigation bar

The bottom line of navigation bar (having a search controller) is flickering when I scroll in iOS (iPhone X). Tried many solutions, but none of them worked. Here is my code:
(I'm not using storyboard)
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow()
let viewCtrl = TabController()
let navCtrl = UINavigationController(rootViewController: viewCtrl)
self.window?.rootViewController = navCtrl
self.window?.makeKeyAndVisible()
return true
}
TabBarController
class TabController: UITabBarController {
init() {
super.init(nibName: nil, bundle: nil)
self.viewControllers = [ViewController()]
self.navigationItem.searchController = UISearchController(searchResultsController: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}
}
ViewController
class ViewController: UITableViewController {
init() {
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Test"
return cell
}
}
Help will be highly appreciated.
I have found the same issue, and I am thinking is a bug from Apple. But anyway, if it helps, making the navigation bar isTranslucent = false can help to avoid that ugly behaviour.

Not able to use Custom Cell with UITableView

I am trying to populate uitableview with custom but getting the following error:
Fatal error: Use of unimplemented initializer 'init(style:reuseIdentifier:)' for class 'Appname.PostCellView'
Code:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! PostCellView
return cell
}
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(PostCellView.self, forCellReuseIdentifier: "Cell")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Post View Cell: (this class is loaded in the File's Owner of the view)
import UIKit
protocol PostCellViewDelegate: class {
}
class PostCellView: UITableViewCell {
weak var delegate: PostCellViewDelegate?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let _ = commonInitialization()
}
func customise(imageName : String , color : UIColor, logoLabelValue : String, websiteValue: String)
{
}
func commonInitialization() -> UIView
{
let bundle = Bundle.init(for: type(of: self))
let nib = UINib(nibName: "PostCellView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
return view
}
}
Please help me finding what's wrong with my code and how I should rectify the same.
Override init(style:reuseIdentifier:)
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// Code
}
Please check this tutorial
Please shift the call to commonInitialization() from initWithCoder to method awakeFromNib. Also remove the initWithCoder method
In your PostCellView code add this method and remove the initWithCoder.
override func awakeFromNib() {
super.awakeFromNib()
let _ = commonInitialization()
}

Realm init for storyboard tableview

I'm having a problem implementing a synced realm with a storyboard ViewController.
Specifically I'm unsure how to provide the initializers.
If I create the ViewController programmatically I can use the following and everything works fine. However, I'm working with a storyboard. This code is provided by the realm tutorial.
import UIKit
import RealmSwift
class ItemsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let realm: Realm
let items: Results<Item>
let refresh = UIRefreshControl()
let tableView = UITableView()
var notificationToken: NotificationToken?
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Constants.REALM_URL)
self.realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: syncConfig, objectTypes:[Item.self]))
self.items = realm.objects(Item.self).sorted(byKeyPath: "body", ascending: true)
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
notificationToken?.invalidate()
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Items to Get Done"
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
tableView.frame = self.view.frame
....
This does not work for a storyboard ViewConroller since (as I understand) the storyboard is configuring and initializing the ViewConroller.
My question then is, how and where do I write the init?
You can write realm loading in your ViewDidLoad.
import UIKit
import RealmSwift
class ItemTableViewController: UITableViewController {
var realm!
var items: Results<Item>?
override func viewDidLoad() {
super.viewDidLoad()
self.loadItems()
}
//Readcategory:
func loadItems() {
let syncConfig = SyncConfiguration(user: SyncUser.current!, realmURL: Constants.REALM_URL)
self.realm = try! Realm(configuration: Realm.Configuration(syncConfiguration: syncConfig, objectTypes:[Item.self]))
self.items = realm.objects(Item.self).sorted(byKeyPath: "body", ascending: true)
self.tableView?.reloadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items?.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath) as! SwipeCell
if let item = self.items?[indexPath.row] {
cell.textLabel?.text = item.title
}
return cell
}
}

Pass Data between xib files without storyboard in Swift3

I am new to swift (and new to StackOverflow) and learning to use nib files now as I did quite a bit of learning with storyboard already.
In storyboard we can instantiate segues and unwindSegues to fetch data from one VC to another. How can I do the same WITHOUT using storyboard?
I currently have a scrollview(MainViewController.xib) that loads a tableview(ContactView.xib) on it. I have an add button overlaying the tableview which is currently unresponsive.
I want to be able to load another view (AddContact.xib) when I click on the add button to add a new data to my tableview and then unwind to reflect the new data in the table, but WITHOUT using storyboard.
I can provide my code if needed, but am only looking for someone to point me in the correct direction. I know this can be achieved using Navigation Controllers but I can't seem to find a fitting tutorial for it (most are old and use Obj-C. I am unfamiliar with Obj-C).
I even looked at some similar questions like: this and this
but they failed to answer my question.
Any help is appreciated. Thank you.
My AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainVC: MainViewController? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
mainVC = MainViewController(nibName: "MainViewController", bundle: nil)
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
window!.rootViewController = mainVC
window!.makeKeyAndVisible()
return true
}
MainViewController.swift:
class MainViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let contactView: ContactView = ContactView(nibName: "ContactView", bundle: nil)
let dialerView: DialerView = DialerView(nibName: "DialerView", bundle: nil)
self.addChildViewController(dialerView)
self.scrollView.addSubview(dialerView.view)
dialerView.didMove(toParentViewController: self)
self.addChildViewController(contactView)
self.scrollView.addSubview(contactView.view)
contactView.didMove(toParentViewController: self)
var contactViewFrame : CGRect = contactView.view.frame
contactViewFrame.origin.x = self.view.frame.width
contactView.view.frame = contactViewFrame
self.scrollView.contentSize = CGSize(width: self.view.frame.width * 2, height: self.view.frame.height)
}
}
My ContactView.xib:
class ContactView: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var searchBar: UISearchBar!
var names = ["Rohan", "Rahul", "Sneh", "Paa", "Maa", "Vatsal", "Manmohan"]
var numbers = ["9830000001", "9830000002", "9830000003", "9830000004", "9830000005", "9830000006", "9830000007"]
let navVC: UINavigationController? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.rowHeight = CGFloat(60)
tableView.register(UINib(nibName: "ContactCell", bundle: nil), forCellReuseIdentifier: "Contact")
let cell = tableView.dequeueReusableCell(withIdentifier: "Contact", for: indexPath) as! ContactCell
cell.nameLabel.text = names[indexPath.row]
cell.numLabel.text = numbers[indexPath.row]
return cell
}
// Make delete-able in scroll view
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
names.remove(at: indexPath.row)
numbers.remove(at: indexPath.row)
tableView.reloadData()
}
}
My AddContact.xib only has the outlets defined.
Edit: I found a solution thanks to the commenters.
To Anyone wondering how I did it, I'll post the steps below:
Step 1: Create a protocol (I did so in my MainViewController.xib)
protocol NewContactDelegate {
func add_Contact (name: String, num: String)
}
Step 2: Instantiate a delegate for it in the senderVC. AddContact.xib was the senderVC for me
class AddContact: UIViewController {
var delegate: NewContactDelegate? = nil
#IBOutlet weak var nameField: UITextField!
#IBOutlet weak var numField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func saveContact(_ sender: UIButton) {
if delegate != nil {
if let name = nameField?.text, let num = numField?.text {
delegate?.add_Contact(name: name, num: num)
dismiss(animated: true, completion: nil)
}
}
}
Step 3: Make ReceivingVC conform to the delegate and accept data.
class ContactView: ....., NewContactDelegate {
func add_Contact(name: String, num: String) {
names.append(name)
numbers.append(num)
addressBook.reloadData() // This is my TableView outlet
}
}
**
NOTE: Remember to assert the delegate to a non-nil value in the RecievingVC to make sure #IBAction in SenderVC works as expected.
**

Delegate didSet property of UIView not being called inside custom UITableViewCell

I've been banging my head on the desk for the past 2 hours trying to figure this out so any help will be greatly appreciated.
I'm using CVCalendar (the develop branch) and I'm trying to place the CalendarView inside a custom UITableViewCell so I can make it look something like this:
The problem I'm facing is that it doesn't set the delegate property when I call self.calendarView.calendarDelegate = self inside the custom UITableViewCell
I know it's not being set because I've placed a println inside the delegate didSet property that's not being outputted.
Here is the CalendarViewDelegate outlet inside the CalendarView
// MARK: - Calendar View Delegate
#IBOutlet weak var calendarDelegate: AnyObject? {
set {
if let calendarDelegate = newValue as? Delegate {
delegate = calendarDelegate
println("delegate did set property called")
}
}
get {
return delegate
}
}
Here is my code for the custom UITableViewCell I'm working with.
import UIKit
class CalendarCell: UITableViewCell {
var calendarView: CVCalendarView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backgroundColor = UIColor.redColor()
selectionStyle = .None
self.calendarView = CVCalendarView(frame: self.frame)
self.calendarView.calendarDelegate = self
self.calendarView.calendarAppearanceDelegate = self
self.calendarView.animatorDelegate = self
contentView.addSubview(calendarView)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.calendarView.commitCalendarViewUpdate()
}
}
Here is my UITableViewController code:
import UIKit
class CalendarTableViewController: UITableViewController {
#IBOutlet var myTableView: UITableView!
var items = ["Apple", "Banana"];
override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.registerClass(CalendarCell.self, forCellReuseIdentifier: NSStringFromClass(CalendarCell))
self.myTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.myTableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func creatHeaderCell() -> UITableViewCell {
var headerCell = self.myTableView.dequeueReusableCellWithIdentifier(NSStringFromClass(CalendarCell)) as! CalendarCell
return headerCell
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.section == 0) {
return 300
}
return 44
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0:
return 1
default:
return items.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (indexPath.section == 0) {
return self.creatHeaderCell()
}
let cell = self.myTableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = "Thomas"
// Configure the cell...
return cell
}
}
Your println statement is probably not reached because your if condition if let calendarDelegate = newValue as? Delegate is not true because what you pass in as calenderDelegate is a CalenderCell, not a Delegate or CVCalendarViewDelegate (typealias).
Additionally your CalendarCell does not conform to CVCalendarViewAppearanceDelegate and CVCalendarViewAnimatorDelegate.
You have to make your cell conform to those protocols to be able to set them as delegates.

Resources