2016-09-13 15 views
6

Tôi có Bộ điều khiển Chế độ xem Chia tách và khi người dùng nhấp vào một ô trong Chế độ xem Bảng, ứng dụng sẽ hiển thị Trình điều khiển Chế độ xem với Chế độ xem Vùng chứa và Điều khiển Phân đoạn để chuyển đổi giữa hai bộ điều khiển xem trẻ em.Không thể sắp xếp lại các ô Xem Bộ sưu tập

Bộ điều khiển xem con đầu tiên là Bộ điều khiển chế độ xem bộ sưu tập. Trong mọi ô của Chế độ xem Bộ sưu tập, có một từ.

Quan điểm trông như thế này:

view screen

tôi thêm các điều khiển xem con lập trình với mã này (từ chi tiết Controller, các thùng chứa của subviews):

override func viewDidLoad() { 


    self.currentViewController = self.storyboard?.instantiateViewController(withIdentifier: "WordCollectionViewControllerID") 
    (self.currentViewController as! WordCollectionViewController).text = self.text 
    self.currentViewController?.view.translatesAutoresizingMaskIntoConstraints = false 
    self.addChildViewController(self.currentViewController!) 
    self.addSubview(subView: self.currentViewController!.view, toView: self.textContainerView) 
    self.currentViewController?.view.layoutIfNeeded() 
    self.currentViewController?.didMove(toParentViewController: self) 

    super.viewDidLoad() 

} 

func addSubview(subView: UIView, toView parentView: UIView) { 
    parentView.addSubview(subView) 

    var viewBindingsDict = [String: AnyObject]() 
    viewBindingsDict["subView"] = subView 
    parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[subView]|", options: [], metrics: nil, views: viewBindingsDict)) 
    parentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[subView]|", options: [], metrics: nil, views: viewBindingsDict)) 
} 

đây là mã để chuyển đổi giữa hai chế độ xem con với điều khiển được phân đoạn:

@IBAction func changeChildViewController(_ sender: UISegmentedControl) { 
    if sender.selectedSegmentIndex == 0 { 
     let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "WordCollectionViewControllerID") 
     (newViewController as! WordCollectionViewController).text = self.text 
     newViewController!.view.translatesAutoresizingMaskIntoConstraints = false 
     cycle(from: self.currentViewController!, to: newViewController!) 
     self.currentViewController = newViewController 
     print("Reorder") 
    } else { 
     let newViewController = self.storyboard?.instantiateViewController(withIdentifier: "NavigationControllerEditTextViewControllerID") 
     (newViewController?.childViewControllers[0] as! EditTextViewController).text = self.text 
     newViewController!.view.translatesAutoresizingMaskIntoConstraints = false 
     cycle(from: self.currentViewController!, to: newViewController!) 
     self.currentViewController = newViewController 
     print("Edit") 
    } 
} 

func cycle(from: UIViewController, to: UIViewController) { 
    from.willMove(toParentViewController: nil) 
    self.addChildViewController(to) 

    self.addSubview(subView: to.view, toView: self.textContainerView) 

    to.view.layoutIfNeeded() 

    from.view.removeFromSuperview() 

    from.removeFromParentViewController() 
    to.didMove(toParentViewController: self) 
} 

Để có thể sắp xếp lại các tế bào tôi đã thực hiện phương pháp này cho các lớp học CollectionViewController tùy chỉnh của tôi:

override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { 

    // Swap values if sorce and destination 
    let change = textArray[sourceIndexPath.row] 


    textArray.remove(at: sourceIndexPath.row) 
    textArray.insert(change, at: destinationIndexPath.row) 


    // Save changes in Core Data 
    text?.reordered_text = textArray.joined(separator: " ") 

    (UIApplication.shared.delegate as! AppDelegate).saveContext() 


    collectionView.reloadData() 
} 

Vấn đề là khi tôi nhấp vào bảng Xem di động và các ứng dụng cho thấy các container View với bộ sưu tập View Controller được nhúng, tôi không thể sắp xếp lại các ô và để có thể thực hiện việc này, tôi phải thay đổi Child (bằng Điều khiển phân đoạn) và sau đó quay lại Chế độ xem bộ sưu tập.

Làm cách nào để giải quyết vấn đề này?

+0

Xin lỗi nếu tôi đã thực hiện một số sai lầm với ngôn ngữ, tôi tiếng ý – ale00

+0

Bạn đang làm gì trên tableView của bạn -didSelect? – Aks

+0

Tôi không làm gì trên tableView-didSelect @Aks – ale00

Trả lời

0

tôi sẽ kiểm tra hai điều:

Đầu tiên bạn gọi willMoveToParentViewController trong phương pháp cycle(from: UIViewController, to: UIViewController) của bạn, nhưng không phải trong viewDidLoad, hãy cố gắng làm điều này. Từ những gì tôi có thể đọc, bạn có thể gọi cycle trong phương thức viewDidLoad của mình, gọi phương thức trên Nil -object (from) sẽ không làm gì cả.

Thứ hai kiểm tra xem bộ sưu tập WordCollectionViewControllerXem delegatedataSource của bạn có được đặt đúng khi tải lần đầu không. Đây có thể là những lúc khó khăn khi tải từ StoryBoard nếu các yếu tố không được khởi tạo kịp thời.

+0

Cảm ơn bạn, tôi đã triển khai các thay đổi này nhưng không có gì thay đổi – ale00

Các vấn đề liên quan