UITableView compiling error in AppDelegate Swift - ios

This is my Viewcontroller:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let list = ["Facebook: 2000$", "Twitter: 10000$", "Vine: 23356$", "Uber: 35000", "Adobe Systems: 400900", "Agilent Tech: 456700", "Ebay: 98899"]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
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.
}
}
and my app delegate:
import UIKit
import CoreData
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) { etc..
What am I doing wrong?

It may be missing-
override func viewDidLoad() {
super.viewDidLoad()
yourTableView.delegate = self
yourTableView.dataSource = self
}

Related

Application crashed while downloading SVGImage using SDWebImageSVGCoder

I'm using SVG image in my project which is coming from API, as I know there's no direct method to use,load or download SVG image directly into iOS Swift app So I'm using third party library which is SDWebImageSVGCoder. But I'm facing two issues, First app is crashing while download specific image and second it's written in Objective-C. I'm not good in Objective-C so I tried alot to understand about the issue and I also tried some other libraries but they also not helping. I need help because I'm stuck there and app is crashing everytime and I have no clue. Thanks
Here's my code
import SDWebImageSVGCoder
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let SVGCoder = SDImageSVGCoder.shared
SDImageCodersManager.shared.addCoder(SVGCoder)
return true
}
}
import UIKit
import SDWebImageSVGCoder
struct Bank:Decodable {
var icon: String?
var name: String?
}
class ViewController: UIViewController {
var banks: [Bank] = [Bank(icon: "https://identity.moneyhub.co.uk/bank-icons/virgin", name: "Virgin Money")]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}
func setupTableView() {
self.tableView.delegate = self
self.tableView.dataSource = self
}
}
//MARK: - TABLEVIEW DELEGATE DATASOURCE
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return banks.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: BankTableViewCell.identifier, for: indexPath) as! BankTableViewCell
cell.configure(self.banks[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 62
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
class BankTableViewCell: UITableViewCell {
#IBOutlet weak var bankNameLabel: UILabel!
#IBOutlet weak var bankLogoImageView: UIImageView!
class var identifier: String {
return "BankTableViewCell"
}
func configure(_ bank: Bank) {
self.bankNameLabel.text = bank.name
guard let url = URL(string: bank.icon ?? "") else {return}
bankLogoImageView.sd_imageIndicator = SDWebImageActivityIndicator.gray
bankLogoImageView.sd_setImage(with: url)
}
}
I want to find out why this library is crashing I posted question on pod Github but they're not responding. Kindly give me any suggestion, solution or alternative library which I can use and solve my problem. Thanks
You are using an outdated library that is not supported anymore. To resolve the crash you will need to use SDWebImageSVGNativeCoder. It can be downloaded here:
https://github.com/SDWebImage/SDWebImageSVGNativeCoder

How to fix iOS10 safe area and topLayoutGuide issue?

I have a question about safe area.
And I write a code and build in two simulators of iOS version 10.2 and 11.4.
It shows different part of red rectangle area like following image.
What's wrong with me?
Thanks.
code here:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UINavigationController(rootViewController: ViewController())
window?.makeKeyAndVisible()
return true
}
}
class ViewController: UIViewController {
let tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "TITLE HERE"
tableView.dataSource = self
tableView.delegate = self
self.view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.left.equalTo(10)
make.top.equalTo(self.topLayoutGuide.snp.bottom)
make.right.bottom.equalTo(-10)
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "\(indexPath) TEST"
return cell
}
}

SearchResultsController obscures screen when used with UINavigationItem

I'm using new integrated UISearchBar available in iOS 11, the problem is as soon as I tap on the searchbar and start typing text, the SearchResultsController obscures the whole screen, including the searchbar. It is impossible to dismiss the results controller or cancel search afterwards.
To demonstrate the issue, I've configured a minimal reproducible example:
import UIKit
let reuseid = "reuseIdentifier"
class TableViewController: UITableViewController, UISearchResultsUpdating {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseid)
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseid, for: indexPath)
return cell
}
func updateSearchResults(for searchController: UISearchController) {
print(searchController.searchBar.text)
}
}
class ViewController: UIViewController {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
searchController = UISearchController(searchResultsController: TableViewController())
navigationItem.searchController = searchController
}
}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
let nc = UINavigationController(rootViewController: ViewController())
window?.rootViewController = nc
return true
}
}
Initial state
Tapping on the SearchBar
The screen is obscured by the UITableView - colored in green
What could cause this bug and how can it be avoided?
Solved by adding this to the SearchResultsUpdater:
edgesForExtendedLayout = []
With default edges, the ResultsController tries to extend its view beyond the UINavigationBar and hence, obscures it:

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.
**

create a UITableViewController programmatically in Swift

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

Resources