https://www.letsbuildthatapp.com/course/intermediate-training-core-data
Friday, 28 June 2019
Intermediate training course
https://www.letsbuildthatapp.com/course/intermediate-training-core-data
Expandable collapse UITableView
Best tutorial in swift
https://www.letsbuildthatapp.com/course_video?id=2242
http://vasundharavision.com/blog/ios/expandable-tableview-without-third-party
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let kHeaderSectionTag: Int = 6900;
@IBOutlet weak var tableView: UITableView!
var expandedSectionHeaderNumber: Int = -1
var expandedSectionHeader: UITableViewHeaderFooterView!
var sectionItems: Array<Any> = []
var sectionNames: Array<Any> = []
override func viewDidLoad() {
super.viewDidLoad()
sectionNames = [ "iPhone", "iPad", "Apple Watch","iPhone", "iPad", "Apple Watch","iPhone", "iPad", "Apple Watch","iPhone", "iPad", "Apple Watch","iPhone", "iPad", "Apple Watch" ];
sectionItems = [ ["iPhone 5", "iPhone 5s", "iPhone 6", "iPhone 6 Plus", "iPhone 7", "iPhone 7 Plus"],
["iPad Mini", "iPad Air 2", "iPad Pro", "iPad Pro 9.7"],
["Apple Watch", "Apple Watch 2", "Apple Watch 2 (Nike)"], ["iPhone 5", "iPhone 5s", "iPhone 6", "iPhone 6 Plus", "iPhone 7", "iPhone 7 Plus"],
["iPad Mini", "iPad Air 2", "iPad Pro", "iPad Pro 9.7"],
["Apple Watch", "Apple Watch 2", "Apple Watch 2 (Nike)"], ["iPhone 5", "iPhone 5s", "iPhone 6", "iPhone 6 Plus", "iPhone 7", "iPhone 7 Plus"],
["iPad Mini", "iPad Air 2", "iPad Pro", "iPad Pro 9.7"],
["Apple Watch", "Apple Watch 2", "Apple Watch 2 (Nike)"], ["iPhone 5", "iPhone 5s", "iPhone 6", "iPhone 6 Plus", "iPhone 7", "iPhone 7 Plus"],
["iPad Mini", "iPad Air 2", "iPad Pro", "iPad Pro 9.7"],
["Apple Watch", "Apple Watch 2", "Apple Watch 2 (Nike)"], ["iPhone 5", "iPhone 5s", "iPhone 6", "iPhone 6 Plus", "iPhone 7", "iPhone 7 Plus"],
["iPad Mini", "iPad Air 2", "iPad Pro", "iPad Pro 9.7"],
["Apple Watch", "Apple Watch 2", "Apple Watch 2 (Nike)"]
];
self.tableView!.tableFooterView = UIView()
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Tableview Methods
func numberOfSections(in tableView: UITableView) -> Int {
if sectionNames.count > 0 {
tableView.backgroundView = nil
return sectionNames.count
} else {
let messageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height))
messageLabel.text = "Retrieving data.\nPlease wait."
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = .center;
messageLabel.font = UIFont(name: "HelveticaNeue", size: 20.0)!
messageLabel.sizeToFit()
self.tableView.backgroundView = messageLabel;
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.expandedSectionHeaderNumber == section) {
let arrayOfItems = self.sectionItems[section] as! NSArray
return arrayOfItems.count;
} else {
return 0;
}
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if (self.sectionNames.count != 0) {
return self.sectionNames[section] as? String
}
return ""
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0;
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
return 0;
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
//recast your view as a UITableViewHeaderFooterView
let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = UIColor.colorWithHexString(hexStr: "#0075d4")
header.textLabel?.textColor = UIColor.white
if let viewWithTag = self.view.viewWithTag(kHeaderSectionTag + section) {
viewWithTag.removeFromSuperview()
}
let headerFrame = self.view.frame.size
let theImageView = UIImageView(frame: CGRect(x: headerFrame.width - 32, y: 13, width: 18, height: 18));
theImageView.image = UIImage(named: "Chevron-Dn-Wht")
theImageView.tag = kHeaderSectionTag + section
header.addSubview(theImageView)
// make headers touchable
header.tag = section
let headerTapGesture = UITapGestureRecognizer()
headerTapGesture.addTarget(self, action: #selector(ViewController.sectionHeaderWasTouched(_:)))
header.addGestureRecognizer(headerTapGesture)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as UITableViewCell
let section = self.sectionItems[indexPath.section] as! NSArray
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.text = section[indexPath.row] as? String
return cell
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
// MARK: - Expand / Collapse Methods
@objc func sectionHeaderWasTouched(_ sender: UITapGestureRecognizer) {
let headerView = sender.view as! UITableViewHeaderFooterView
let section = headerView.tag
let eImageView = headerView.viewWithTag(kHeaderSectionTag + section) as? UIImageView
if (self.expandedSectionHeaderNumber == -1) {
self.expandedSectionHeaderNumber = section
tableViewExpandSection(section, imageView: eImageView!)
} else {
if (self.expandedSectionHeaderNumber == section) {
tableViewCollapeSection(section, imageView: eImageView!)
} else {
let cImageView = self.view.viewWithTag(kHeaderSectionTag + self.expandedSectionHeaderNumber) as? UIImageView
tableViewCollapeSection(self.expandedSectionHeaderNumber, imageView: cImageView!)
tableViewExpandSection(section, imageView: eImageView!)
}
}
}
func tableViewCollapeSection(_ section: Int, imageView: UIImageView) {
let sectionData = self.sectionItems[section] as! NSArray
self.expandedSectionHeaderNumber = -1;
if (sectionData.count == 0) {
return;
} else {
UIView.animate(withDuration: 0.4, animations: {
imageView.transform = CGAffineTransform(rotationAngle: (0.0 * CGFloat(Double.pi)) / 180.0)
})
var indexesPath = [IndexPath]()
for i in 0 ..< sectionData.count {
let index = IndexPath(row: i, section: section)
indexesPath.append(index)
}
self.tableView!.beginUpdates()
self.tableView!.deleteRows(at: indexesPath, with: UITableView.RowAnimation.fade)
self.tableView!.endUpdates()
}
}
func tableViewExpandSection(_ section: Int, imageView: UIImageView) {
let sectionData = self.sectionItems[section] as! NSArray
if (sectionData.count == 0) {
self.expandedSectionHeaderNumber = -1;
return;
} else {
UIView.animate(withDuration: 0.4, animations: {
imageView.transform = CGAffineTransform(rotationAngle: (180.0 * CGFloat(Double.pi)) / 180.0)
})
var indexesPath = [IndexPath]()
for i in 0 ..< sectionData.count {
let index = IndexPath(row: i, section: section)
indexesPath.append(index)
}
self.expandedSectionHeaderNumber = section
self.tableView!.beginUpdates()
self.tableView!.insertRows(at: indexesPath, with: UITableView.RowAnimation.fade)
self.tableView!.endUpdates()
}
}
}
PageViewController swift
UIPageViewController
This is your story board file
This is your story board file
I have 3 types of VC's , 1st Vc, 2nd VC is
PageViewController
with 3 pages(I added PageController
to normal ViewController
here), third one is FinalViewController
.
1st VC has button when i click it will move to 2nd VC that is
PageViewController
(It has 3 pages[3 ViewConrollers
] working fine).
In
PageViewController
after loading first VC, it has view with gestures when i tap that i want to navigate FinalViewController
completely.
My 1st ViewController code
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.navigationBar.isHidden = false
}
@IBAction func btn(_ sender: Any) {
let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "NVC")
self.navigationController?.pushViewController(storyboard!, animated: true)
}
}
My NewViewController code
import UIKit
class NewViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
@IBOutlet weak var pagerController: UIPageControl!
@IBOutlet weak var pageControllerView: UIView!
// The pages it contains
var pages = [UIViewController]()
// The UIPageViewController
var pageContainer: UIPageViewController!
// Track the current index
var currentIndex: Int?
private var pendingIndex: Int?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the pages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1 = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "ThirdViewController")
pages.append(page1)
pages.append(page2)
pages.append(page3)
page1.variable = "This is strig..."
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
pageControllerView.addSubview(pageContainer.view)
// Configure our custom pageControl
view.bringSubviewToFront(pagerController)
pagerController.numberOfPages = pages.count
pagerController.currentPage = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - UIPageViewController delegates
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == 0 {
return nil
}
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = pages.firstIndex(of:viewController)!
if currentIndex == pages.count-1 {
return nil
}
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
pendingIndex = pages.firstIndex(of:pendingViewControllers.first!)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed {
currentIndex = pendingIndex
if let index = currentIndex {
pagerController.currentPage = index
}
}
}
}
My ViewController code
import UIKit
class ViewController: UIViewController {
var variable = String()
@IBOutlet weak var subView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
subView.isUserInteractionEnabled = true
//Add gesture to incoming call view
let incomingCallsViewTap = UITapGestureRecognizer(target: self, action: #selector(incomingCallsViewTapFunction(_:)))
self.subView!.addGestureRecognizer(incomingCallsViewTap)
}
// Tap gestrure selector fuction
@objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
// self.navigationController?.pushViewController(clvc, animated: false)
(UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
}
@IBAction func btn(_ sender: Any) {
print("ViewController one")
print(variable)
}
}
Subscribe to:
Posts (Atom)
Difference between == and ===
Difference between == and === https://stackoverflow.com/questions/24002819/difference-between-and == operator checks if their ...
-
Upload image to server in Swift 4.1 and Xcode 9.4.1 Here i'm posting only Upload image code. In this below code you need to ...
-
UIPageViewController This is your story board file I have 3 types of VC's , 1st Vc, 2nd VC is PageViewController with 3 pag...
-
Best tutorial in swift https://www.letsbuildthatapp.com/course_video?id=2242 http://vasundharavision.com/blog/ios/expandable-table...