ios – How do I make the segue to the another ViewController with data transfer from TableViewCell

[ad_1]

I have a tableview in which there are custom cells, in each custom cell there is a collection view and in this collection view there is a UICollectionReusableView with a button. How do I make the segue to the another ViewController with data transfer from TableViewCell. Also I have segue from collectionCell to another ViewController by closure and it is work.

This is my TableViewController

class TaskListViewController: UITableViewController, UICollectionViewDelegate {

@IBOutlet var activityIndicator: UIActivityIndicatorView!

var taskLists: [TaskList] = []

let db = Firestore.firestore()


let user: User! = {
    guard let currentUser = Auth.auth().currentUser else { return nil }
    return User(user: currentUser)
}()


override func viewDidLoad() {
    super.viewDidLoad()
    
    activityIndicator.isHidden = false
    activityIndicator.startAnimating()
    
    
    updateTaskList(user)

}

//method works (for closure)

func moveOnTaskViewController(tIndex: Int, cIndex: Int, task: Task, taskList: TaskList) {
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    guard let newVC = storyBoard.instantiateViewController(withIdentifier: "TestViewController") as? TestViewController else { return }
    
    newVC.task = task 
    newVC.taskList = taskList
    
    
    navigationController?.present(newVC, animated: true
}



override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "TasksListCell", for: indexPath) as! NewTableViewCell
    let task = taskLists[indexPath.row]
    cell.configure(with: task)
    cell.taskList = task
    

    cell.didSelectClosure = { tabIndex, collIndex in
        let currentTask = task.tasks[collIndex ?? 0]
        self.moveOnTaskViewController(tIndex: indexPath.row, cIndex: collIndex ?? 0, task: currentTask, taskList: task)
        
        
    }
    return cell
}

Code from TableViewCell

typealias DidSelectClosure = ((_ tableIndex: Int?, _ collectionIndex: Int?) -> Void)

class NewTableViewCell: UITableViewCell {

@IBOutlet var taskNameLabel: UILabel!
@IBOutlet var tasksCollection: UICollectionView!

@IBOutlet var progressOfDoneTasks: UIProgressView!

@IBOutlet var progressOfDoneTasksLabel: UILabel!



var didSelectClosure: DidSelectClosure?
var index: Int?




var taskList: TaskList! {
    didSet {
        taskNameLabel.text = taskList.name
        tasksCollection.reloadData()
    }
}

let sectionInserts = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)


override func awakeFromNib() {
    super.awakeFromNib()
    
    tasksCollection.delegate = self
    tasksCollection.dataSource = self
    

}


 



func configure(with taskList: TaskList) {

    
    var current: [Task] {
        taskList.tasks.filter { $0.isComplete  }
    }
    
    let totalProgress = Float(current.count) / Float(taskList.tasks.count)
    progressOfDoneTasks.setProgress(totalProgress, animated: true)

    progressOfDoneTasksLabel.text = "\(current.count) из \(taskList.tasks.count)"
}

}

extension NewTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    taskList.tasks.count == 0 ? 0 : taskList.tasks.count
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) as! NewCollectionViewCell
    
    if taskList.tasks.count != 0 {
    cell.taskNameLabel.text = taskList.tasks[indexPath.row].name
    }
    cell.backgroundColor = UIColor(ciColor: CIColor(red: 147/255, green: 211/255, blue: 4/255, alpha: 0.4))
    cell.layer.cornerRadius = 20
    

    return cell
}



func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    
    if let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "section", for: indexPath) as? SectionHeaderReusableView {
        return sectionHeader
    }
    
   
       return UICollectionReusableView()
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    didSelectClosure?(index, indexPath.row)
}

}

[ad_2]

Source link

Leave a Reply

Your email address will not be published.