Adding views to Cell, not working properly
Hello,
I am using Version 7.0.4. I want to add events from database on cell, then tried to adding view to cellview. It shows correct cells

but after scroll up and down, these dots randomly added random cells.
also added some random cells

This is my function to adding dots:
func setColorfulDots(view: JTAppleCell?, cellState: CellState) {
guard let validCell = view as? EventCell else {return}
self.filteredDots = events.filter(
{
(json) -> Bool in return
((json.dictionaryValue["start_date"]?.date)!.equalToDate(dateToCompare: cellState.date) ||
(json.dictionaryValue["end_date"]?.date)!.equalToDate(dateToCompare: cellState.date)) ||
(cellState.date.isGreaterThanDate(dateToCompare: (json.dictionaryValue["start_date"]?.date)!) &&
cellState.date.isLessThanDate(dateToCompare: (json.dictionaryValue["end_date"]?.date)!))
})
countOfCellDots = filteredDots.count
for (index, perDot) in filteredDots.enumerated() {
dotViewWidth = (dotWidth * countOfCellDots) + (2 * (countOfCellDots - 1))
var X = (Int(validCell.width) - dotViewWidth) / 2
X = X + (dotViewWidth / (countOfCellDots * 2)) + (dotViewWidth / countOfCellDots) * index
dotView = UIView(frame: CGRect(
x: X,
y: Int(validCell.dayLabel.frame.origin.y + validCell.dayLabel.frame.size.height),
width: dotWidth,
height: dotWidth))
dotView.layer.cornerRadius = CGFloat(dotWidth / 2)
dotView.backgroundColor = hexStringToUIColor(hex: perDot["color"].stringValue)
validCell.contentView.addSubview(dotView)
validCell.selectedView.isHidden = false
}
filteredDots = []
}
//Calling from cellForItemAt:
func calendar(_ calendar: JTAppleCalendarView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "eventCell", for: indexPath) as! EventCell
if cell.dayLabel != nil
{
cell.dayLabel.text = cellState.text
setColorfulDots(view: cell, cellState: cellState)
}
return cell
}
@patchthecode, please see this.
@Saruul-Ulzii you said it shows correct cells.. what is the problem?
let me know what was expected, and let me know what is wrong, and i can help.
@patchthecode pls see pics, correct cells are red-backgrounded, little colorful dots must be in these red-backgrounded cells, after launch 1st pic[2017.07] is correct, but after scrolling another months up and down, these little dots adding another cells, like shown in 2 picture, its mustn't, then returning month 2017.07, now many cells has colorful dots, not only correct red cells.
validCell.contentView.addSubview(dotView)
validCell.selectedView.isHidden = false
1st line is adding dots, 2nd line is showing red background, if little dots added to cell, it must be has red-background, because these 2 line work sametime, there isnt any condition, but now why just adding little dots?
@Saruul-Ulzii its late here my time. So i will take a look at this in the morning. But before I take a look at it, have you seen this video yet? It shows you an example how to set up dots and events.
I will take a look in the morning.
@Saruul-Ulzii looking at your code, this is not the way you add views to a cell. I think this playlist will help you. I do not have all of your code, but it looks like your code is not correct.
I think you should delete the view added (if any) when dequeue delegate is called, or else it will keep the view when dequeue of the cell that has the view is dequeued.
A better way is to add the view in your custom cell, and set the isHidden as true as default, and change it to false when there is a event at dequeue.
@patchthecode i watched all tutorial videos. Red-background is like tutorial's selected view. i just want to add views, not toggle with isHidden - true/false, isHidden-false is working properly, then trying to add views, cause i don't know how many dots need to add to cell, then i can't create dot views on cell in storyboard. Its too easy to test your code, you need to add some views to cell on handleCellSelection method in tutorial.
@Saruul-Ulzii ok, can yu join me here? -> https://gitter.im/patchthecode/JTAppleCalendar
Maybe I might have misunderstood the problem.
problem resolved. DotViews were being added to the views, but was not being removed.