Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // NewsTableViewController.swift
- // ShkabaiProject
- //
- // Created by Admin on 23/08/2018.
- // Copyright © 2018 Admin. All rights reserved.
- //
- import UIKit
- import Alamofire
- import PINRemoteImage
- private let reuseHeadCellNews = "headNewsCell"
- private let reuseIndicCellNews = "indicLoadNewsCell"
- class NewsTableViewController: UITableViewController {
- var loadDataShkabai = LoadDataShkabai()
- var arrayNameCategoryNews: [String] = []
- var arrayNewsCategory: [[tjeratNews]] = []
- var opensection: [String: Bool] = [:]
- var height = 0
- var tapCell: IndexPath = IndexPath(row: 10, section: 10)
- var itemHeights: [String: CGFloat] = [:]
- var test = NewsTableViewCell()
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView.register(UINib(nibName: "NewsTableViewCell", bundle: nil), forCellReuseIdentifier: reuseHeadCellNews)
- // tableView.register(UINib(nibName: "NewsUpdateTableViewCell", bundle: nil), forCellReuseIdentifier: reuseIndicCellNews)
- //tableView.delegate = NewsTableViewCell()
- tableView.estimatedRowHeight = 160
- NotificationCenter.default.addObserver(self, selector: #selector(reloadTableVC), name: NSNotification.Name(rawValue: "reloadTableVC"), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(beginUpdateTableVC), name: NSNotification.Name(rawValue: "beginUpdateTableVC"), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(endUpdateTableVC), name: NSNotification.Name(rawValue: "endUpdateTableVC"), object: nil)
- loadDataShkabai.parseTjeratJson {
- self.arrayNameCategoryNews = self.loadDataShkabai.arrayNameCategoryNews
- self.arrayNewsCategory = self.loadDataShkabai.arrayNewsCategory
- self.tableView.reloadData()
- }
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- UIView.performWithoutAnimation {
- tableView.beginUpdates()
- tableView.endUpdates()
- }
- }
- override func numberOfSections(in tableView: UITableView) -> Int {
- return arrayNewsCategory.count
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return arrayNewsCategory[section].count
- }
- override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- return arrayNameCategoryNews[section]
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let headCell = tableView.dequeueReusableCell(withIdentifier: reuseHeadCellNews, for: indexPath)
- //let indicatorCell = tableView.dequeueReusableCell(withIdentifier: reuseIndicCellNews, for: indexPath)
- if arrayNewsCategory.count != 0{
- if let cell = headCell as? NewsTableViewCell{
- cell.nameChanelLabel.text = arrayNewsCategory[indexPath.section][indexPath.row].name
- if (isSectionOpened(indexPath.section,indexPath.row)) {
- cell.symbolCellLabel.text = "⌄"
- }else{
- cell.symbolCellLabel.text = "⌃"
- }
- cell.imageChanel.pin_setImage(from: URL(string: arrayNewsCategory[indexPath.section][indexPath.row].logo))
- if let k = Int(arrayNewsCategory[indexPath.section][indexPath.row].kolNews) {
- cell.loadDataNews(url: self.arrayNewsCategory[indexPath.section][indexPath.row].url, numberNews: k, indexPath: indexPath)
- }
- return cell
- }
- }
- return headCell
- }
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- changeSectionsStatus(indexPath.section, indexPath.row)
- tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
- tapCell = indexPath
- }
- override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
- itemHeights["\(indexPath.section)\(indexPath.row)"] = cell.frame.size.height
- }
- override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if isSectionOpened(indexPath.section, indexPath.row) {
- return UITableViewAutomaticDimension
- }
- if let h = itemHeights["\(indexPath.section)\(indexPath.row)"] {
- return h
- }
- return 61
- }
- override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
- if let h = itemHeights["\(indexPath.section)\(indexPath.row)"] {
- tableView.beginUpdates()
- tableView.endUpdates()
- return h
- }
- return UITableViewAutomaticDimension
- }
- @objc func reloadTableVC(_ notification: NSNotification){
- if let index = notification.userInfo![0] as? IndexPath {
- if index == tapCell {
- //tableView.cellForRow(at: index)?.autoresizingMask
- // tableView.beginUpdates()
- // tableView.endUpdates()
- }
- }
- }
- @objc func beginUpdateTableVC() {
- tableView.beginUpdates()
- }
- @objc func endUpdateTableVC() {
- tableView.endUpdates()
- }
- override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
- guard let header = view as? UITableViewHeaderFooterView else { return }
- header.backgroundView?.backgroundColor = UIColor.white
- header.textLabel?.textColor = UIColor.lightGray
- header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24)
- header.textLabel?.frame = header.frame
- }
- func isSectionOpened(_ section: Int, _ row: Int) -> Bool{
- if let status = opensection["\(section)\(row)"]{
- return status
- }
- return false
- }
- func changeSectionsStatus(_ section: Int, _ row: Int){
- if let status = opensection["\(section)\(row)"]{
- opensection["\(section)\(row)"] = !status
- }
- else{
- opensection["\(section)\(row)"] = true
- }
- }
- func closeAllSectionRows(nowSecOpen: String){
- if opensection.count != 0{
- for some in opensection{
- if (nowSecOpen != some.key){
- opensection[nowSecOpen] = false
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement