카테고리 없음

[공부기록]100일_46일차

lingk 2020. 10. 22. 22:52

1. Life 버킷 달성 - 형광펜

thisYear에서 줄긋기와 비슷한 방법을 사용하였다😊

attributedString을 사용하여 PlusBucketVC에서 체크표시를 할 경우, DetailLifeVC에서 다음 함수를 실행시킨다

    func highlight(index:Int)->NSAttributedString{
        let target=DataManger.shared.life[cellNum][index]
        let attributedString = NSMutableAttributedString(string: target.want ?? "")
        print(target.iDidIt)
        if target.iDidIt == true{
            attributedString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor(displayP3Red: 0.793, green: 0.822, blue: 0.898, alpha: 1), range: _NSRange(location:0,length:target.want!.count))
            
        }
        return attributedString
    }
    

}

 

 

2. 셀의 내용이 없을때도 연속되는 다이나믹 셀

3. 버킷리스트가 없을때

 

섹션을 이용하여 만들어지지 않은 셀이 나타나는 것을 막아주었다(?)

섹션을 2개만들어주었다. 

    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

섹션 1은 헤더를 이용하여 셀들을 가려주는 것이기 때문에 셀을 만들어주지 않고, 섹션 0일때에만 셀을 생성해주었다

이때, thisYear가 가지고 있는 데이터 숫자에 따라서 버킷리스트의 유무를 판단했다

데이터가 없을때 셀을 한개 만들어주어 버킷리스트를 추가해달라는 텍스트를 넣어줄 것이다

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            if DataManger.shared.thisYear.count == 0{
                return 1
            }else{
                return DataManger.shared.thisYear.count
            }}else{
                return 0
            }
    }

다음은 헤더와 관련된 함수이다

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
        headerView.backgroundColor = UIColor.white
        
        return headerView
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if(section == 0) {
            return 0
        }
        else{
            return self.view.bounds.height
        }
        
    }

 

버킷리스트가 있는 상태에서 모두 지워주면 버킷리스트를 추가해 달라는 내용이 출력되어야한다.

이 과정에서 꽤 많은 어려움이 있었다,,,❗️❗️

delete를 통해서 마지막 남은 한개의 버킷리스트를 지우면 셀의 개수는 0이다.

하지만 0번째 셀의 라벨에 "버킷리스트를 입력해주세요"를 넣어야한다.

0번째 셀은 버킷리스트를 지우면서 사라지기 때문에 접근할 수 없다😑

이를 수정하기 위해서 다음 함수에 조건을 추가하고 notification을 사용하여 테이블뷰를 리로드해주었다.

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete{
            
            let bucketList = DataManger.shared.thisYear[indexPath.row]
            DataManger.shared.deleteTYB(_bucket: bucketList)
            //테이블뷰에 표시된 셀 숫자와 배열의 데이터 숫자는 일치해야한다
            DataManger.shared.thisYear.remove(at: indexPath.row)
            
            
            if DataManger.shared.thisYear.count == 0{
                NotificationCenter.default.post(name: PlusTYBViewController.newBucketInsert, object: nil)
            }else{
                tableView.deleteRows(at: [indexPath], with: .bottom)
            }
            
        }
        print("here")
    }

버킷리스트가 없을때, 셀이 선택되면 안된다❗️따라서 didSelect함수에도 조건을 추가해주었다

 

이제 앱 기능적인 부분은 끝❗️인줄 알았으나 새로운 문제들 발견

 

우선 헤더의 height에 따라서 스크롤이 되는 정도가 달라진다

이부분은 계산을 몇번 해보면서 해결 아주 뿌듯 정말 뿌듯 너무 뿌듯

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if(section == 0) {
            return 0
        }
        else{
            if (self.tableView.bounds.height - CGFloat(DataManger.shared.thisYear.count * 80) - 80)>0{
                return self.tableView.bounds.height - CGFloat(DataManger.shared.thisYear.count * 80)
                }else{
                    return 80
                }
        }
        
    }

 

근데 또 다른 문제

thisYear의 FirstVC에서 체크가 되어있는 셀을 지우고 지워진 셀 자리에 들어간 버킷리스트를 체크하면 터진다

이건 고쳐야할부분,,고민

 

반응형