[ad_1]
I’m at present growing a Health Utility and dealing on Exercise View. It has a performance just like to-do guidelines. So principally the consumer is marking Units of a sure train that he completed. I do know that it’s wanted to arrange TableViewCells for reuse, and I did that for the state when all of the units of train is finished and consumer should not be capable to work together whit that TableViewCell anymore. The one factor that retains getting duplicated is numberOfRowsInSection. Guys please assist me discover the lacking half, I’ve been attempting for 2 days at this level:(
TableView is contained in the collectionViewCell so I’m initializing it from there. I’m utilizing customized delegate as I’ve to have an entry to tableView from the view that holds collectionView.
protocol TableViewCellDelegate: AnyObject {
func tableView(tableViewCell: WorkoutStartedTVCurrentExerciseCell, index: Int, didTappedInTableViewCell: WorkoutStartedCell)
// different delegate strategies that you could outline to carry out motion in viewcontroller
}
class WorkoutStartedCell: UICollectionViewCell {
weak var cellDelegateTV: TableViewCellDelegate?
let scrollView: UIScrollView = {
let view = UIScrollView()
view.backgroundColor = UIColor.clear
view.isScrollEnabled = true
view.contentMode = .heart
view.showsHorizontalScrollIndicator = false
view.isMultipleTouchEnabled = true
view.isUserInteractionEnabled = true
return view
}()
let titleLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor.clear
label.textColor = UIColor.black
label.textAlignment = .heart
label.textual content = "Incline Bench Press"
label.font = UIFont.systemFont(ofSize: 23.0, weight: .semibold)
return label
}()
let numberOfExercisesLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor.clear
label.textColor = UIColor.black.withAlphaComponent(0.7)
label.textAlignment = .heart
label.font = UIFont.systemFont(ofSize: 16.5, weight: .medium)
return label
}()
let container: UIView = {
let view = UIView()
view.layer.cornerRadius = 10
view.backgroundColor = UIColor.black
view.layer.shadowOffset = CGSize(width: 0, peak: 0)
view.clipsToBounds = false
view.layer.shadowOpacity = 0.35
view.layer.shadowRadius = 10
view.layer.shadowColor = UIColor.black.cgColor
return view
}()
let tableView: UITableView = {
let tableView = UITableView()
tableView.backgroundColor = UIColor.clear
tableView.separatorStyle = .none
tableView.register(WorkoutStartedTVCurrentExerciseCell.self, forCellReuseIdentifier: "currentExerciseCellID")
tableView.layer.cornerRadius = 0
tableView.scrollIndicatorInsets = UIEdgeInsets(high: 0, left: 0, backside: 0, proper: 0)
tableView.contentInset = UIEdgeInsets(high: 0, left: 0, backside: 0, proper: 0)
tableView.showsHorizontalScrollIndicator = false
tableView.showsVerticalScrollIndicator = true
tableView.isScrollEnabled = true
return tableView
}()
var number_of_sets : Int!
var number_of_reps : Int!
var completed_sets = [Int]()
var remaining_sets = [Int]()
//var current_set: IndexPath!
//var completedSets: [Int]!
override init(body: CGRect) {
tremendous.init(body: body)
backgroundColor = UIColor.white
contentView.isUserInteractionEnabled = true
isMultipleTouchEnabled = true
//tableView.estimatedRowHeight = 0
//tableView.estimatedSectionHeaderHeight = 0
//tableView.estimatedSectionFooterHeight = 0
//tableView.delegate = self
//tableView.dataSource = self
tableView.isScrollEnabled = false
contentView.addSubview(scrollView)
scrollView.addSubview(titleLabel)
scrollView.addSubview(numberOfExercisesLabel)
scrollView.addSubview(container)
scrollView.addSubview(tableView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: heightAnchor).precedence = UILayoutPriority.defaultLow
//scrollView.delegate = self
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.topAnchor.constraint(equalTo: scrollView.topAnchor, fixed: 25).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
titleLabel.sizeToFit()
numberOfExercisesLabel.translatesAutoresizingMaskIntoConstraints = false
numberOfExercisesLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, fixed: 10).isActive = true
numberOfExercisesLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
numberOfExercisesLabel.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
numberOfExercisesLabel.sizeToFit()
container.translatesAutoresizingMaskIntoConstraints = false
container.topAnchor.constraint(equalTo: numberOfExercisesLabel.bottomAnchor, fixed: 15).isActive = true
container.centerXAnchor.constraint(equalTo: centerXAnchor, fixed: 0).isActive = true
container.widthAnchor.constraint(equalToConstant: self.body.width - 0).isActive = true
container.heightAnchor.constraint(equalToConstant: self.body.width - 0).isActive = true
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: container.bottomAnchor, fixed: 20).isActive = true
tableView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
tableView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
override func prepareForReuse() {
tremendous.prepareForReuse()
number_of_sets = nil
number_of_reps = nil
remaining_sets.removeAll()
completed_sets.removeAll()
tableView.delegate = nil
tableView.dataSource = nil
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been carried out")
}
}
extension WorkoutStartedCell: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
return remaining_sets.depend + completed_sets.depend
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "currentExerciseCellID", for: indexPath) as! WorkoutStartedTVCurrentExerciseCell
cell.selectionStyle = .none
cell.Set.textual content = "(Array(1...number_of_sets)[indexPath.row])"
cell.Reps.textual content = "(number_of_reps ?? 1)"
if completed_sets.depend == number_of_sets {
cell.isUserInteractionEnabled = false
} else {
cell.isUserInteractionEnabled = true
}
if completed_sets.accommodates(the place: {$0 == indexPath.row}) == false, indexPath.row == remaining_sets.first {
//Present Set
cell.Set.textColor = #colorLiteral(purple: 0, inexperienced: 0.9810667634, blue: 0.5736914277, alpha: 1).withAlphaComponent(0.75)
cell.SetStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.Weight.textColor = #colorLiteral(purple: 0, inexperienced: 0.9810667634, blue: 0.5736914277, alpha: 1).withAlphaComponent(0.75)
cell.WeightStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.Reps.textColor = #colorLiteral(purple: 0, inexperienced: 0.9810667634, blue: 0.5736914277, alpha: 1).withAlphaComponent(0.75)
cell.RepsStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.CompletedImage.tintColor = UIColor.white
cell.CompletedImage.backgroundColor = UIColor.white
cell.CompletedImage.layer.borderColor = UIColor.darkGray.withAlphaComponent(0.9).cgColor
} else if completed_sets.accommodates(indexPath.row) == true, remaining_sets.accommodates(indexPath.row) == false {
//Accomplished Set
cell.Set.textColor = UIColor.black.withAlphaComponent(0.7)
cell.SetStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.Weight.textColor = UIColor.black.withAlphaComponent(0.7)
cell.WeightStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.Reps.textColor = UIColor.black.withAlphaComponent(0.7)
cell.RepsStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.CompletedImage.tintColor = UIColor.white
cell.CompletedImage.backgroundColor = UIColor.darkGray.withAlphaComponent(0.9)
cell.CompletedImage.layer.borderColor = UIColor.darkGray.withAlphaComponent(0.9).cgColor
} else if completed_sets.accommodates(indexPath.row) == false, remaining_sets.accommodates(indexPath.row) == true, indexPath.row != remaining_sets.first {
//Remaining Units
cell.Set.textColor = UIColor.black.withAlphaComponent(1.0)
cell.SetStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.Weight.textColor = UIColor.black.withAlphaComponent(1.0)
cell.WeightStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.Reps.textColor = UIColor.black.withAlphaComponent(1.0)
cell.RepsStatic.textColor = UIColor.black.withAlphaComponent(0.7)
cell.CompletedImage.tintColor = UIColor.white
cell.CompletedImage.backgroundColor = UIColor.white
cell.CompletedImage.layer.borderColor = UIColor.darkGray.withAlphaComponent(0.9).cgColor
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return false
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as? WorkoutStartedTVCurrentExerciseCell
self.cellDelegateTV?.tableView(tableViewCell: cell!, index: indexPath.row, didTappedInTableViewCell: self)
}
}
Right here is how I move the information from ViewController and setting CollectionView delegate and knowledge supply.
`
extension workoutStartedView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection part: Int) -> Int {
return exercise_names.depend
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let exercisesCount = Array(1...exercise_names.depend)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! WorkoutStartedCell
cell.number_of_reps = Int(number_of_reps[indexPath.item])!
cell.number_of_sets = Int(number_of_sets[indexPath.item])!
cell.remaining_sets = remainingSets[indexPath.item]
cell.completed_sets = completedSets[indexPath.item]
cell.titleLabel.textual content = exercise_names[indexPath.item]
cell.numberOfExercisesLabel.textual content = "Train (exercisesCount[indexPath.item]) of (exercise_names.depend)"
cell.cellDelegateTV = self
cell.tableView.heightAnchor.constraint(equalToConstant: 75 * CGFloat(Int(self.number_of_sets[indexPath.item])!)).isActive = true
cell.scrollView.contentSize = CGSize(width: collectionView.body.width, peak: 100 + (collectionView.body.width) + (75 * CGFloat(Int(self.number_of_sets[indexPath.item])!)))
cell.scrollView.scrollToTop(animated: false)
cell.tableView.delegate = cell.self
cell.tableView.dataSource = cell.self
cell.tableView.reloadData()
//cell.layoutIfNeeded()
cell.tag = Array(0...exercise_names.depend - 1)[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, insetForSectionAt part: Int) -> UIEdgeInsets {
return sectionInsets
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width - 40, peak: collectionView.bounds.peak)
}
func collectionView(_ collectionView: UICollectionView, structure collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt part: Int) -> CGFloat {
return 10
}
}
`
Right here I’m dealing with knowledge from collectionViewCell throw customized delegate
`
extension workoutStartedView: TableViewCellDelegate {
func tableView(tableViewCell: WorkoutStartedTVCurrentExerciseCell, index: Int, didTappedInTableViewCell: WorkoutStartedCell) {
//if didTappedInTableViewCell.remaining_sets.depend > 1 {
didTappedInTableViewCell.completed_sets.append(didTappedInTableViewCell.remaining_sets.first!)
didTappedInTableViewCell.remaining_sets.removeFirst()
if completedSets.depend > 0 {
if self.completedSets.depend > didTappedInTableViewCell.tag {
self.completedSets[didTappedInTableViewCell.tag] = didTappedInTableViewCell.completed_sets
} else if self.completedSets.depend <= didTappedInTableViewCell.tag {
self.completedSets.append(didTappedInTableViewCell.completed_sets)
}
} else if completedSets.depend <= 0 {
self.completedSets.append(didTappedInTableViewCell.completed_sets)
}
if self.remainingSets[didTappedInTableViewCell.tag].depend > 0 {
self.remainingSets[didTappedInTableViewCell.tag] = didTappedInTableViewCell.remaining_sets
} else if self.remainingSets[didTappedInTableViewCell.tag].depend <= 1 {
self.remainingSets.removeFirst()
}
print(didTappedInTableViewCell.completed_sets)
print(didTappedInTableViewCell.remaining_sets)
print(self.completedSets)
print(self.remainingSets)
//collectionView.reloadData()
didTappedInTableViewCell.tableView.reloadData()
}
}
`
[ad_2]
