I have already set up in the viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
the tableView use static cell and the UIScrollViewDelegate is already part of the UITableViewController...
I made some research on this issue and every time it was because of the delegate not being set. But I did it and the func is still not being called.
Any idea on what could be the issue?
Stupid error.
I just had to change
func scrollViewDidScroll(scrollView: UIScrollView) {
to
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
Related
So I have a UIViewController that have a collectionView with horizontal scrolling (like the facebook stories on the top of newsFeed page) and under it is the tableView with cells. How can I hide this collection view when I scroll down the tableView? I want it to exactly like facebook.
You have to set collectionView height constraint, and when you begin dragging table view:
heightConstraint.constant = 0
You can know about start dragging from table view delegate. For this you should inherit your viewController from UITableViewDelegate and set a function :
class ViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("begin dragging")
heightConstraint.constant = 0
}
}
I am trying to call a method after my scroll animation is complete. I have a Table View and I set the delegate in viewDidLoad like thisself.tableView.delegate = self based on this , but that didn't help.
Basically, If a button is clicked, it scrolls up to a specific cell, lets call it the start cell, in the table view if the start cell is not visible. I'm using the scrollToRow method for the scrolling and animation
tableViewController.tableView.scrollToRow(at: IndexPath(row: techniqueNo, section: 0), at: UITableViewScrollPosition.top, animated: true)
Once this scroll animation is complete, I want to add an animation to the start cell. I tried adding this method from the scroll view delegate, but it isn't getting called
func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
print("animation Complete")
}
Any idea why this method is not getting called?
Note: This is different from UITableView is done scrolling because
(1) this is swift and that answer is Objective-C, and
(2) I have tried implementing the method from the delegate to check if the animation finished, but its not getting called and I need help figuring out why
Is this what you need?
Add these funcs to your table view controller...
Edit: forgot to include the scrollViewDidEndScrollingAnimation ...
override func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
scrollingFinish()
}
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollingFinish()
}
override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
scrollingFinish()
}
}
func scrollingFinish() -> Void {
print("Scroll Finished!")
}
As I run the code I am successfully feeding JSON data into my application at the rendering of this associated UIView. I have a tableView adequately linked to this file, when I run the application I see the tableview itself. BUT none of the 3 datasource functions are automatically getting called - the 'wtf' print statement, after 2 hours of playing with this, will.not.print.
am I missing something? This code is identical to all other tableviewdatasource code I've written and this simply won't work or render the JSON data I am supplying it with
does anyone have thoughts or suggestions? Thanks!
import UIKit
class ChooseUserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var users: [BackendlessUser] = []
override func viewDidLoad() {
super.viewDidLoad()
loadUsers()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("wtf")
return users.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("chooseCell", forIndexPath: indexPath)
let user = users[indexPath.row]
print("wtf!")
cell.textLabel?.text = user.email
return cell
}
}
You have tableview with datasource and delegate connected with view controller in storyboard(Interface Builder).
In case you're forgotten to set the dataSource and delegate in your Storyboard using Ctrl+Drag you need to specify in your viewDidLoad that you're the dataSource and delegate like in the following way:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
loadUsers()
}
I hope this help you.
The Answer: I had not connected the delegate and the datasource of my table object to the view controller - OOPS! I feel silly but that was clearly my issue
I'm trying to have a better understanding on how the dataSource and delegate outlets get connected to the UITableView under the hood when you do the connection through the UI in Xcode by dragging and dropping to the viewController icon.
I found this thread but I think I'm missing something because I cannot make it work.
Here is the code I currently have that works fine by connecting the outlets through XCode (by drag and dropping).
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var hobbies:[String] = ["Computers", "Photography", "Cars", "Reading", "Learning New Things"]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return hobbies.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = hobbies[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.
}
}
I tried removing the outlet connections made by XCode, created an outlet for the tableView (myTable) and added the following code in the viewDidLoad method but it doesn't work, no error it just doesn't load the data.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTable.delegate = self
myTable.dataSource = self
}
Can someone describe the steps needed to do this connection with code?
Just for reference here are the steps needed to do your connection programmatically.
1.- Create outlet for tableView
#IBOutlet weak var myTable: UITableView!
2.- Assign delegate and dataSource in the viewDidLoad method.
myTable.delegate = self
myTable.dataSource = self
3.- DONE
There are a couple of ways to do it.
1. The simplest one is by dragging and dropping:
In your main.storyboard select your TableView;
Press your Control button;
Click and drag the mouse from your TableView to your ViewController's icon and drop it;
Then select dataSource and delegate as shown on the image above.
2. The other way is by coding:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
}
PS: Make sure not to forget connecting your tableView outlet to your code by dragging and dropping it into your ViewController class.
PPS: It'll also ask you to implement the following methods into your class so that your tableView works properly:
numberOfRowsInSection
cellForRowAtIndexPath
For those who don't have them yet, you'll see Xcode complaining about it.
I am very new to Swift and for the last few days I have been trying to figure out how I can implement infinite scroll in tableView which is placed inside a ViewController. All the examples I have found are based on TableViewController but my tableView is placed inside a ViewController.
Actually I am using the library MMDrawerLayout to get a left and right sliding menu so need to use a ViewController. Please some one guide me in the right direction. Any code or sample projects will be much appreciated.
Thanks.
First of all drag and drop tableview into your viewController in storyBoard after that create an Outlet for your tableview like this:
#IBOutlet weak var tableView: UITableView!
And add your tableArray:
var items: [String] = ["We", "Heart", "Swift"]
After that add UITableViewDataSource, UITableViewDelegate at your class declaration and it look like:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//Your code
}
After that conform dataSource and deleget to self in your viewDidLoad method and add thi line in it:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
After that your method will be:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
After that add required method of UITableViewDataSource as shown below:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
return cell
}
For more Info follow THIS tutorial.
To Add infinity indicator view in bottom of your UITableView then followed the following things.
Step 1 : Add following method in your current class.
func infinityScrollView() {
var bounds = UIScreen.mainScreen().bounds
var width = bounds.size.width
var height = bounds.size.height
var footerView = UIView(frame: CGRectMake(0, 0, width, 44))
footerView.backgroundColor=UIColor.greenColor()
var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
actInd.center = CGPointMake(width/2 , 22)
actInd.color = UIColor.redColor()
footerView.addSubview(actInd)
YOUR_TABLEVIEW_OBJECT.tableFooterView = footerView;
}
Step 2 : Called the above method from viewDidLoad.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.infinityScrollView()
}
Hope this help you.