iOS spreadsheetview not able to display multiple blocks in cell

213 Views Asked by At

I’m using Spreadsheetview library in order to show case Jobber functionality. There is one critical issues which is blocking project release. This is mentioned below:

Problem: Not able to display multi block content in custom cell.

Scenario: Suppose userA has task1 from 10:00 AM to 12:00 AM, userB has task2 from 10:00 AM to 11:00 AM, userC has task3 from 10:00 AM to 11:30 AM so these three task should be displayed in merged cell with one after another.

Refer below screenshot.

Screenshot

Code:

func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {

        if self.jobDetails == nil {
            return nil
        }
. . . 

//other cases handled like displaying time, date, visit person name which is not having any issue
. . .

else if case (1...(calenderData.count + 1), 2...(Constants.timeIntervals.count + 1)) = (indexPath.column, indexPath.row) {

 let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: String(describing: ScheduleCell1.self), for: indexPath) as! ScheduleCell1
            if(cell.firstBlockLabel.text != nil) || (cell.secondBlockLabel.text != nil) || (cell.thirdBlockLabel.text != nil) {
                return nil
            }

let visits = calenderData[indexPath.column - 1].calendarRows
            for visit in visits {
                let diff = findTimeDifference(firstTime: cellTime,secondTime: visit.startTime)
                print("startTime: \(visit.startTime) endTime \(visit.endTime) title \(visit.title) totalBlocks \(visit.totalBlocks)")
                
                if(diff >= -30 && diff <= 30 && diff != -1) {
                    switch visit.totalBlocks {
                    case 0,1:
                        cell.firstBlockLabel.isHidden = false
                        cell.secondBlockLabel.isHidden = true
                        cell.thirdBlockLabel.isHidden = true
                        
                        cell.firstBlockLabel.text = "1 - case 1"
                        if visit.blockSerialNo == 1 {
                            if(visit.statusCode.caseInsensitiveCompare("completed") == .orderedSame){
                                cell.firstBlockLabel.attributedText = "\(visit.title)".strikeThrough()
                            } else {
                                cell.firstBlockLabel.text = "\(visit.title)"
                            }
                            
                            cell.firstBlockLabel.backgroundColor = hexStringToUIColor(hex: visit.statusTagProp.background)
                            cell.firstBlockLabel.textColor = hexStringToUIColor(hex: visit.statusTagProp.text)
                        }
                    case 2:
                        cell.firstBlockLabel.isHidden = false
                        cell.secondBlockLabel.isHidden = false
                        cell.thirdBlockLabel.isHidden = true
                        
                        cell.firstBlockLabel.text = "1 - case 2"
                        cell.secondBlockLabel.text = "2 - case 2"
                        
                        if visit.blockSerialNo == 2 {
                            if(visit.statusCode.caseInsensitiveCompare("completed") == .orderedSame){
                                cell.secondBlockLabel.attributedText = "\(visit.title)".strikeThrough()
                            } else {
                                cell.secondBlockLabel.text = "\(visit.title)"
                            }
                            
                            cell.secondBlockLabel.backgroundColor = hexStringToUIColor(hex: visit.statusTagProp.background)
                            cell.secondBlockLabel.textColor = hexStringToUIColor(hex: visit.statusTagProp.text)
                        }
                    case 3:
                        cell.firstBlockLabel.isHidden = false
                        cell.secondBlockLabel.isHidden = false
                        cell.thirdBlockLabel.isHidden = false
                        
                        cell.firstBlockLabel.text = "1 - case 3"
                        cell.secondBlockLabel.text = "2 - case 3"
                        cell.thirdBlockLabel.text = "3 - case 3"
                        
                        if visit.blockSerialNo == 3 {
                            if(visit.statusCode.caseInsensitiveCompare("completed") == .orderedSame){
                                cell.thirdBlockLabel.attributedText = "\(visit.title)".strikeThrough()
                            } else {
                                cell.thirdBlockLabel.text = "\(visit.title)"
                            }
                            
                            cell.thirdBlockLabel.backgroundColor = hexStringToUIColor(hex: visit.statusTagProp.background)
                            cell.thirdBlockLabel.textColor = hexStringToUIColor(hex: visit.statusTagProp.text)
                        }
                    default:
                        break
                    }

break
                }
            }
            return cell
}
        return nil
    }



class ScheduleCell1: Cell {
    let firstBlockLabel = UILabel()
    let secondBlockLabel = UILabel()
    let thirdBlockLabel = UILabel()
    let stackview = UIStackView()
    
    let lineLabel = UILabel()
    var lineYPosition: Int = 0
    
   override var frame: CGRect {
        didSet {
            firstBlockLabel.frame = CGRect(x: 0, y: 0, width: 500, height: 500)
            secondBlockLabel.frame = CGRect(x: 0, y: 0, width: 500, height: 500)
            thirdBlockLabel.frame = CGRect(x: 0, y: 0, width: 500, height: 500)
            
            lineLabel.frame = bounds.insetBy(dx: 0, dy: 0)
            lineLabel.frame = CGRect(x: 0, y: lineYPosition, width: 300, height: 1)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        lineLabel.frame = bounds
        lineLabel.backgroundColor = .red
        
        firstBlockLabel.textAlignment = .center
        //firstBlockLabel.text = "firstBlockLabel"
        firstBlockLabel.numberOfLines = 0
        firstBlockLabel.lineBreakMode = .byTruncatingTail
        firstBlockLabel.translatesAutoresizingMaskIntoConstraints = false
        
        secondBlockLabel.textAlignment = .center
        //secondBlockLabel.text = "secondBlockLabel"
        secondBlockLabel.numberOfLines = 0
        secondBlockLabel.lineBreakMode = .byTruncatingTail
        secondBlockLabel.translatesAutoresizingMaskIntoConstraints = false
        
        thirdBlockLabel.textAlignment = .center
        //thirdBlockLabel.text = "thirdBlockLabel"
        thirdBlockLabel.numberOfLines = 0
        thirdBlockLabel.lineBreakMode = .byTruncatingTail
        thirdBlockLabel.translatesAutoresizingMaskIntoConstraints = false
        
        stackview.frame = bounds
        stackview.axis = .horizontal
        stackview.spacing = .leastNonzeroMagnitude
        stackview.contentMode = .scaleToFill
        stackview.translatesAutoresizingMaskIntoConstraints = false
        stackview.alignment = .fill
        stackview.distribution = .fill
        stackview.distribution = .fillProportionally
        
        stackview.addArrangedSubview(firstBlockLabel)
        stackview.addArrangedSubview(secondBlockLabel)
        stackview.addArrangedSubview(thirdBlockLabel)
        
        firstBlockLabel.backgroundColor = .yellow
        secondBlockLabel.backgroundColor = .purple
        thirdBlockLabel.backgroundColor = .green
        stackview.backgroundColor = .magenta
        //contentView.backgroundColor = .magenta
        
        contentView.addSubview(lineLabel)
        contentView.bringSubviewToFront(lineLabel)
        contentView.addSubview(stackview)
        
        stackview.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
        stackview.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

So I’ve added two property totalBlocks (determines how may blocks to be displayed) and BlockSrNo (determines which serial number of block). Logic for this is mentioned below:

func determineMultiBlocks() {
        for data in calenderData {
            let visits = data.calendarRows

for var i in 0..<visits.count {
                let visit = visits[i]
                
                for var j in (i+1)..<visits.count {
                    let nextVisit = visits[j]

let timeOverlapExists = CheckIfTimeExistBetweenTwoTimeInterval(withStartTime: visit.startTime.timeInSeconds, withEndTime: visit.endTime.timeInSeconds, withTimeToCheck: nextVisit.startTime.timeInSeconds)

if timeOverlapExists {

visit.totalBlocks = visit.totalBlocks + 1
                        nextVisit.totalBlocks = 0 //nextVisit.totalBlocks - 1
                        nextVisit.blockSerialNo = visit.totalBlocks
                        
                        j = j + 1
}
                }
break
            }

Can help me where am I going wrong? If there is any other solution instead of using totalBlocks/blockSerialNo then let me know.

Appreciate all solutions!

0

There are 0 best solutions below