spreadsheet

hayeon·2020년 11월 20일
0

iOS

목록 보기
2/3
//
//  ViewController.swift
//  MySpreadsheetView
//
//  Created by 이하연 on 2020/11/20.
//

import UIKit
import SpreadsheetView

class ViewController: UIViewController, SpreadsheetViewDataSource, SpreadsheetViewDelegate {
    
    let data = [" ","A","B","C","D","E","F","G"]
    
    private let spreadsheetView = SpreadsheetView()
        
    override func viewDidLoad() {
        super.viewDidLoad()
        spreadsheetView.register(MyLabelCell.self, forCellWithReuseIdentifier: MyLabelCell.identifier)
        spreadsheetView.gridStyle = .solid(width:  1, color: .link)
        spreadsheetView.delegate = self
        spreadsheetView.dataSource = self
        
        view.addSubview(spreadsheetView)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        spreadsheetView.frame = CGRect(x: 0, y: 100, width: view.frame.size.width, height: view.frame.size.height-100)
    }
    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, cellForItemAt indexPath: IndexPath) -> Cell? {
        let cell = spreadsheetView.dequeueReusableCell(withReuseIdentifier: MyLabelCell.identifier, for: indexPath) as! MyLabelCell
        
        

        if indexPath.row == 0 {
            // first row
            cell.setupfix(with: data[indexPath.section])
            cell.backgroundColor = .systemGray2
        }
        else if indexPath.section == 0 {
            // first column
            cell.setupfix(with: "\(indexPath.row)")
            cell.backgroundColor = .systemGray2
        }
        else {
            cell.setup(with: "\(indexPath)")
            cell.backgroundColor = .systemBackground
        }
    
        return cell
    }
    
    
    func numberOfColumns(in spreadsheetView: SpreadsheetView) -> Int {
        return data.count
    }
    
    func numberOfRows(in spreadsheetView: SpreadsheetView) -> Int {
        return 200
    }
    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, widthForColumn column: Int) -> CGFloat {
        return 200
    }
    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, heightForRow row: Int) -> CGFloat {
        return 55
    }

    
    func spreadsheetView(_ spreadsheetView: SpreadsheetView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }

}

class MyLabelCell: Cell {
    
    static let identifier = "MyLabelCell"
    
    private let label = UILabel()
    
    public func setup(with text: String) {
        label.text = text
        label.textAlignment = .left
        contentView.addSubview(label)
    }
    public func setupfix(with text: String) {
        label.text = text
        label.textAlignment = .center
        contentView.addSubview(label)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        label.frame = contentView.bounds
    }
    
}
//
//  ViewController.swift
//  Spreadsheet
//
//  Created by 이하연 on 2020/11/15.
//

import UIKit

class SpreadSheetViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    let numList = [1,2,3,4,5,6,7]
    
    //    override func viewDidLoad() {
    //        super.viewDidLoad()
    //        // Do any additional setup after loading the view.
    //    }
    
    @IBOutlet weak var collectionView : UICollectionView!
    
    override func viewDidLoad() {
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.reloadData()

    }
    
    // 갯수
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 350
    }
    
    // 어떻게 표현?
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TestCollectionCell", for: indexPath)
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let margin: CGFloat = 2
        let width: CGFloat = (collectionView.bounds.width - margin) / 8
        let height: CGFloat = 30
        
        return CGSize(width: width, height: height)
    }
    



}

0개의 댓글