2017-06-01 11 views
4

タイムテーブル(UITableView)と日曜バー(UICollectionView)で構成されるアプリがあります。テーブルビューの下部にある「JumpToTomorrow」ボタンをクリックしてDayBar まだ画面に表示されていないセルのプロパティを変更するにはどうすればよいですか?

  • から日を選択し

    1. :ユーザーには2つの方法のいずれかによって、それらの選択日を変更することができます。

    2の問題は、次の日がまだ画面にスクロールされていないシナリオでクリックすると、その日のセルが選択されていないことです(下記参照)。 enter image description here

    DayCollectionViewCell.isSelected(何らかの理由で呼び出されていない)

    override var isSelected: Bool { 
        didSet { 
         self.backgroundColor  = self.getBackgroundColor(selected: isSelected) 
         self.layer.borderColor  = self.getBorderColor(selected: isSelected) 
         self.number.attributedText = NSAttributedString(string: number.text!, attributes: self.getTextAttributes(selected: isSelected)) 
        } 
    } 
    

    DayBarController機能

    override func viewDidLoad() { 
        super.viewDidLoad() 
        // To preserve selection between presentations 
        self.clearsSelectionOnViewWillAppear = false 
        setEdgeInsets() 
        BroadcastManager.listen(to: PrepNotifications.JumpToTomorrowClicked, listenerId: "UpdateSelectedDay", react: updateSelectedDay) 
    } 
        /// change the selected day and notify the app about the change 
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
        PrepGlobals.Calendar.SELECTED_DAY = indexPath.row 
        BroadcastManager.broadcast(PrepNotifications.SelectedDayChanged) 
        //scroll to center on selected day in case of clicked on today 
        if (PrepGlobals.Calendar.TODAY_INDEX == indexPath.row){ 
         self.collectionView?.scrollToItem(
          at: indexPath, 
          at: .centeredHorizontally, 
          animated: true 
         ) 
        } 
    } 
    
    private func updateSelectedDay(_ userInfo: [AnyHashable: Any]? = nil) { 
        PrepGlobals.Calendar.SELECTED_DAY += 1 
        let selectedDayIndex = IndexPath(row: PrepGlobals.Calendar.SELECTED_DAY, section: 0) 
        self.collectionView!.selectItem(at: selectedDayIndex, animated: true, scrollPosition: .right) 
        BroadcastManager.broadcast(PrepNotifications.SelectedDayChanged) 
    } 
    
  • 答えて

    1

    私は自分の答えを編集しました。実際にはisSelectedを手動で設定する必要があります。

    override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { 
         cell.isSelected = (indexPath.row == PrepGlobals.Calendar.SELECTED_DAY) 
    } 
    
    +0

    これも機能します。 – Deco

    1

    あなたは、セルインデックスが選択されなければならない確認するようにcollectionView(_:cellForItemAt:)を変更する必要がありますそうでない場合は、スタイルを更新してください。

    +0

    これは、必要なセルには呼び出されていないようです。私の質問の中に描かれている例では、 'collectionView(_cellForItemAt)'がセル5と7に対して呼び出されます。選択範囲を5から6に変更しようとしているとき。 – Deco

    +0

    プリフェッチのように見えると、画面に表示されたり、「選択済み」に設定されています。 – Deco

    1

    これを修正したプリフェッチはこれを修正しました。

    enter image description here

    関連する問題