2017-04-05 24 views
0

我正在使用UICollectionViewdidSelectItemAt更新我在我的HomeController中設置的UI標籤。所以基本上當我按下單元格時,我希望我的文本被更新。然而,文本不更新,我已經調試,但值改變。我已經嘗試了一切,無論是setNeedsDisplay()和貫穿main thread(它已經這樣做了)即使它具有新值,UILabel文本仍未更新,已嘗試了所有內容

我有相當多的這些類別代碼(我不使用故事板),但這是標籤設置(坐在HomeController中):

import UIKit 
import CoreData 

class mainHomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 
static let sharedInstance = HomeController() 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     setupBasketBar() 

    } 

    let totNumber: UILabel = { 
     let label = UILabel() 
     label.text = "0" 
     label.numberOfLines = 2 
     return label 
    }() 

    func setupBasketBar() { 

     self.view.addSubview(totNumber) 
     totNumber.translatesAutoresizingMaskIntoConstraints = false 
     totNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 330).isActive = true 
     totNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 
     totNumber.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true 
     totNumber.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 5).isActive = true 


    } 

} 

這個坐落在饋電單元:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    HomeController.sharedInstance.totNumber.text = ("234") 
    HomeController.sharedInstance.totNumber.setNeedsDisplay() 


} 

我已經編輯了相關部分。

這是HomeController的整個代碼:

import UIKit 
import CoreData 

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    static let sharedInstance = HomeController() 

    let cellId = "cellId" 
    let trendingCellId = "trendingCellId" 
    let subscriptionCellId = "subscriptionCellId" 

    let titles = ["Home", "Trending", "Subscriptions", "Account"] 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     navigationController?.navigationBar.isTranslucent = false 

     let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: view.frame.height)) 
     titleLabel.text = "Home" 
     titleLabel.textColor = UIColor.black 
     titleLabel.font = UIFont.systemFont(ofSize: 20) 
     navigationItem.titleView = titleLabel 

     setupCollectionView() 
     setupMenuBar() 
     setupBasketBar() 

    } 

    func setupCollectionView() { 
     if let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout { 
      flowLayout.scrollDirection = .horizontal 
      flowLayout.minimumLineSpacing = 0 
     } 

     collectionView?.backgroundColor = UIColor.white 
     collectionView?.register(FeedCell.self, forCellWithReuseIdentifier: cellId) 
     collectionView?.register(TrendingCell.self, forCellWithReuseIdentifier: trendingCellId) 
     collectionView?.register(SubscriptionCell.self, forCellWithReuseIdentifier: subscriptionCellId) 


     collectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0) 
     collectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0) 

     collectionView?.isPagingEnabled = true 
    } 

    lazy var settingsLauncher: SettingsLauncher = { 
     let launcher = SettingsLauncher() 
     launcher.homeController = self 
     return launcher 
    }() 

    func handleMore() { 
     //show menu 
     settingsLauncher.showSettings() 
    } 

    func showControllerForSetting(_ setting: Setting) { 
     let dummySettingsViewController = UIViewController() 
     dummySettingsViewController.view.backgroundColor = UIColor.white 
     dummySettingsViewController.navigationItem.title = setting.name.rawValue 
     navigationController?.navigationBar.tintColor = UIColor.white 
     navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
     navigationController?.pushViewController(dummySettingsViewController, animated: true) 
    } 

    func handleSearch() { 
     scrollToMenuIndex(2) 

    } 

    func scrollToMenuIndex(_ menuIndex: Int) { 
     let indexPath = IndexPath(item: menuIndex, section: 0) 
     collectionView?.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(), animated: true) 

     setTitleForIndex(menuIndex) 
    } 


    fileprivate func setTitleForIndex(_ index: Int) { 
     if let titleLabel = navigationItem.titleView as? UILabel { 
      titleLabel.text = " \(titles[index])" 
     } 

    } 

    lazy var menuBar: MenuBar = { 
     let mb = MenuBar() 
     mb.homeController = self 
     return mb 
    }() 

    func buttonAction(sender: UIButton!) { 
     let btnsendtag: UIButton = sender 
     if btnsendtag.tag == 1 { 

      let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 

      let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "NewViewController") as! UINavigationController 

      self.present(vc, animated: true, completion: nil) 

     } 

    } 

    let totLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21)) 
    let btn: UIButton = UIButton(frame: CGRect(x: 0, y: 600, width: 500, height: 80)) 

    func setupBasketBar() { 

     //Checkout button 
     btn.backgroundColor = UIColor.rgb(36, green: 51, blue: 70) 

     btn.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) 
     btn.tag = 1 
     self.view.addSubview(btn) 
     btn.setTitleColor(UIColor.yellow, for: .normal) 

     //Button that updates the current amount 

     totLabel.text = "Total amount" 
     totLabel.textColor = UIColor.white 
     self.view.addSubview(totLabel) 

     //constraints 

     totLabel.translatesAutoresizingMaskIntoConstraints = false 
     totLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true 
     totLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 
     totLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true 
     totLabel.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1).isActive = true 


     //oldconstraints 

     /* self.view.addSubview(totNumber) 
     totNumber.translatesAutoresizingMaskIntoConstraints = false 
     totNumber.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 330).isActive = true 
     totNumber.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true 
     totNumber.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: 0).isActive = true 
     totNumber.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1).isActive = true*/ 


    } 

    fileprivate func setupMenuBar() { 
     navigationController?.hidesBarsOnSwipe = false 

     let redView = UIView() 
     redView.backgroundColor = UIColor.rgb(36, green: 51, blue: 70) 
     view.addSubview(redView) 
     view.addConstraintsWithFormat("H:|[v0]|", views: redView) 
     view.addConstraintsWithFormat("V:[v0(50)]", views: redView) 

     view.addSubview(menuBar) 
     view.addConstraintsWithFormat("H:|[v0]|", views: menuBar) 
     view.addConstraintsWithFormat("V:[v0(50)]", views: menuBar) 


     menuBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true 
    } 


    override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { 

     let index = targetContentOffset.pointee.x/view.frame.width 

     let indexPath = IndexPath(item: Int(index), section: 0) 
     menuBar.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) 

     setTitleForIndex(Int(index)) 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 

    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let identifier: String 
     if indexPath.item == 1 { 
      identifier = trendingCellId 
     } else if indexPath.item == 2 { 
      identifier = subscriptionCellId 
     } else { 
      identifier = cellId 
     } 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) 

     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: view.frame.width, height: view.frame.height - 50) 
    } 

} 
+0

的可能的複製[如何使雨燕一個簡單的集合視圖(http://stackoverflow.com/questions/31735228/how-to -ma-simple-collection-view-with-swift) –

+0

歡迎使用stackoverflow!我幾乎可以肯定,使用單例模式(使用'sharedInstance')的方法對於你試圖實現的內容來說是一個更復雜和不必要的方法。 'mainHomeController'是一個拼寫錯誤,還是你的項目同時包含'mainHomeController'和'HomeController'類?在'HomeController'或'mainHomeController'上調用'didSelectItemAt'方法嗎?它可能看起來很多,但如果你可以粘貼整個課程,這可能會有很大的幫助。 – MathewS

+0

謝謝@MathewS!我對編碼也很新,我猜想我想實現的應該會容易得多...是的,只有一個'HomeController' - 是一個錯字,因爲我想讓代碼更清潔,對不起。 我上傳了我的'HomeController'的所有代碼。基本上我通過'JSON'向''collectionViewCells'發送'string array'。然後我使用一個按鈕將數組數據從單元發送到'HomeController'' UILabel' – chetbaker

回答

0

我猜你的sharedInstance變量是視圖控制器的新實例,這是從分配和故事板或顯示在一個單獨的塞格斯。你應該設置sharedInstance等於self裏面viewDidLoad

+0

是的,我也這麼認爲。 'SharedInstance'是一個新的實例,並且具有與'self'不同的內存位置。 – ron27

+0

非常感謝!我懷疑共享實例出了什麼問題。你如何設置完全等於viewDidLoad中的自身的sharedInstance?我是編碼新手,無法真正弄清楚。 – chetbaker

1

好吧!看看更新的代碼片段sharedInstance單身絕對不是必需的 - 我會完全刪除它。

didSelectItemAt方法則只是需要這樣的:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    totNumber.text = "234" 
}