Skip to content

Instantly share code, notes, and snippets.

@shankartshinde
Created May 8, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shankartshinde/0dccad65ebb6e57149ce12d5b81c3715 to your computer and use it in GitHub Desktop.
Save shankartshinde/0dccad65ebb6e57149ce12d5b81c3715 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// ContactApp
//
// Created by Yugan on 08/05/18.
// Copyright © 2018 Yugan. All rights reserved.
//
import UIKit
class ViewController: UITableViewController {
let cellIdentifier = "CellIdentifier"
let nameOfEmployee = ["Shankar","Archana","Amol","Shivaji","Ketan"]
let nameOfPlaces = ["Mumbai","Pune","Dehli","Hyderabad"]
let nameOfAnimals = ["Elephant","Cat","Dog","Cow","Tiger","Lion"]
var twoDiamentionalArray = [
["Shankar","Archana","Amol","Shivaji","Ketan"],
["Mumbai","Pune","Dehli","Hyderabad"],
["Elephant","Cat","Dog","Cow","Tiger","Lion"]
]
let sectionNames = ["Best Friends","Beautiful Cities","Faviorate Animal"]
var showIndexPaths = true
@objc func doAnimationForSection() {
var indexPathsToReload = [IndexPath]()
for section in stride(from: 0, to: twoDiamentionalArray.count, by:1) {
//print("\(index) with value \(value)")
for row in stride(from: 0, to:twoDiamentionalArray[section].count , by:1) {
//print("for section \(section) with row \(row)")
let newIndexPath = IndexPath(row: row, section: section)
indexPathsToReload.append(newIndexPath)
}
}
let animationStyle = showIndexPaths ? UITableViewRowAnimation.left :UITableViewRowAnimation.right
tableView.reloadRows(at: indexPathsToReload, with: animationStyle)
showIndexPaths = !showIndexPaths
for sec in twoDiamentionalArray.indices {
for row in twoDiamentionalArray[sec].indices {
print("for section \(sec) with row \(row)")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//readDataFromLocalBundleFile()
let barButtonItem = UIBarButtonItem(title: "ShowAnimation", style: UIBarButtonItemStyle.plain, target: self, action: #selector(doAnimationForSection))
//navigationController?.navigationItem.rightBarButtonItem = barButtonItem
navigationItem.rightBarButtonItem = barButtonItem
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//extension ViewController:UITableViewDelegate {
//
//}
extension ViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
//return 2
return twoDiamentionalArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//return section == 0 ? nameOfEmployee.count : nameOfPlaces.count
return twoDiamentionalArray[section].count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
let cellText = twoDiamentionalArray[indexPath.section][indexPath.row]
if showIndexPaths {
cell.textLabel?.text = "In Section \(indexPath.section) for row \(indexPath.row) is " + cellText //nameOfEmployee[indexPath.row]
}
else {
cell.textLabel?.text = cellText
}
return cell
}
}
extension ViewController {
// override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
// //return section == 0 ? "Employee Name": "Beautiful Plcaces"
// }
@objc func tableSectionExpandClose(sender:UIButton) {
let section = sender.tag
var indexPathesHideAndShow = [IndexPath]()
for row in twoDiamentionalArray[section].indices {
print("row :\(row) and section \(section)")
let tempIndexPath = IndexPath(row: row, section: section)
indexPathesHideAndShow.append(tempIndexPath)
}
twoDiamentionalArray[section].removeAll()
tableView.deleteRows(at: indexPathesHideAndShow, with: UITableViewRowAnimation.fade)
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// let frame = CGRect(x: 20, y: 0, width: 320, height: 30)
// let headerLabel = UILabel(frame:frame)
// headerLabel.text = sectionNames[section] //section == 0 ? "Employee Name": "Beautiful Plcaces"
// headerLabel.setValue(UIColor.cyan, forKey: "backgroundColor")
// return headerLabel
let button = UIButton(type: UIButtonType.custom)
button.setTitle("Close", for: .normal)
button.backgroundColor = UIColor.yellow
button.tag = section
button.addTarget(self, action: #selector(tableSectionExpandClose), for: .touchUpInside)
return button
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}
extension ViewController {
func readDataFromLocalBundleFile() {
if let urlPath = Bundle.main.url(forResource: "sampleColor", withExtension: "json") {
let jsonData = try? Data(contentsOf: urlPath)
//let finalJSONResult:[String:Any] = try? JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions)
let finalJSONResult = try? JSONSerialization.jsonObject(with: jsonData!, options: [])
if let colorDictionary = finalJSONResult as? [String:Any] {
if let colorInfoList = colorDictionary["colors"] as? [[String:Any]] {
print(colorInfoList)
for element in colorInfoList {
if let code = element["code"] as? [String:Any] {
print("Element have type of object [String:Any] is :\(code)")
}
if let type = element["type"] as? String {
print("Element contain key 'type' of type of object is String: \(type)")
}
if let category = element["category"] as? String {
print("Element contain key 'category' of type of object is String: \(category)")
}
if let color = element["color"] as? String {
print("Element contain key 'color' of type of object is String: \(color)")
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment