๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/iOS

[iOS]Swift๋กœ ์—‘์…€ํŒŒ์ผ ํŒŒ์‹ฑํ•ด์„œ ํ…Œ์ด๋ธ”๋ทฐ์— ๋„์šฐ๊ธฐ(Parse Excel with Swift)

by Fomagran ๐Ÿ’ป 2021. 2. 15.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ํ‰์†Œ์— ์—‘์…€์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ• ์ˆ˜ ์—†์„๊นŒ? ๋ฅผ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€

 

์—‘์…€์„ ํŒŒ์‹ฑํ•˜๋Š”๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ๊ฒŒ๋ผ์„œ ์ •๋ณด๋ฅผ ๊ณต์œ ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Preview

 

 


Excel

 

๋จผ์ € Excel ํŒŒ์ผ์ธ .csv ํŒŒ์ผ์„ ์ค€๋น„ํ•ด์ฃผ์„ธ์š”!

 

์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜๋ผ์™€ ๋„์‹œ ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋ผ์ฝ”๋“œ๊ฐ€ ์žˆ๋Š” ์—‘์…€์ •๋ณด๋ฅผ ์ค€๋น„ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

(ํ˜น์‹œ ํ•„์š”ํ•˜์‹  ๋ถ„๋“ค์„ ์œ„ํ•ด ํŒŒ์ผ์„ ์ฒจ๋ถ€ํ•ด๋†“๊ฒ ์Šต๋‹ˆ๋‹ค!)

 

location.csv
0.50MB

 


Xcode

 

์ด ํŒŒ์ผ์„ Xcode ํ”„๋กœ์ ํŠธ ์•ˆ์— ๋„ฃ์–ด์ฃผ์„ธ์š”!

 

 

 

 

๊ทธ๋Ÿฌ๋ฉด ์—‘์…€์•ˆ์— ์žˆ๋Š” ์ •๋ณด๋“ค์ด Xcode ์•ˆ์—์„  ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณด์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

 


Storyboard

 

๋‚˜๋ผ์ด๋ฆ„๊ณผ ๋„์‹œ๋ฅผ ๋„์šธ ํ…Œ์ด๋ธ”๋ทฐ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด์ค์‹œ๋‹ค!

 

 

 

 

ํ…Œ์ด๋ธ”๋ทฐ์…€์—์„œ Style์„ Subtitle๋กœ ๋ฐ”๊พธ๋ฉด

 

 

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ์ž๋™์œผ๋กœ Title๊ณผ Subtitle์ด ์žˆ๋Š” ์…€์ด ๋งŒ๋“ค์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

 

 

๊ทธ๋ฆฌ๊ณค ExcelTableViewController์™€ ExcelTableViewCell์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”!

 

 

 

 

์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์กŒ๋‹ค๋ฉด

 

 

 

 

๋‹ค์‹œ ์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ์ด๋™ํ•ด์„œ TableViewController ํด๋ž˜์Šค๋ฅผ ExcelTableViewController๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

 

 

 

๊ทธ๋ฆฌ๊ณ  TableViewCell์˜ ํด๋ž˜์Šค๋„ ExcelTableViewCell๋กœ ๋ฐ”๊ฟ”์ฃผ์‹œ๊ณ 

 

 

 

 

๋ฐ˜๋“œ์‹œ Identifier๋„ ์›ํ•˜์‹œ๋Š” ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”.(์ €๋Š” ๊ธฐ์–ตํ•˜๊ธฐ ์‰ฝ๊ฒŒ Class ์ด๋ฆ„์ด๋ž‘ ๋˜‘๊ฐ™์ด ํ–ˆ์Šต๋‹ˆ๋‹ค.)

 

 

 


728x90

ExcelTableViewController

 

์ž ์ด์ œ ExcelTableViewController๋กœ ์ด๋™ํ•ด์ค๋‹ˆ๋‹ค.

 

์ œ์ผ ๋จผ์ € ๋„์‹œ์™€ ๋‚˜๋ผ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

var cityAndCountry:[[String]] = []

๊ทธ๋ฆฌ๊ณค ์—‘์…€ํŒŒ์ผ์„ ํŒŒ์‹ฑํ•˜๋Š” parseCSVAt(url:URL)ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 

url๋กœ ๋ฐ›์€ ์ •๋ณด๋ฅผ data๋กœ ๋ฐ”๊พผ ๋’ค ๋ฐ์ดํ„ฐ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ,์™€ ์ค„๋ฐ”๊ฟˆ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ผ์™€ ๋„์‹œ๋ฅผ cityAndCountry ๋ฐฐ์—ด์— ๋‹ด๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

 private func parseCSVAt(url:URL) {
        do {
            
            let data = try Data(contentsOf: url)
            let dataEncoded = String(data: data, encoding: .utf8)
            
            if let dataArr = dataEncoded?.components(separatedBy: "\n").map({$0.components(separatedBy: ",")}) {
                
                for item in dataArr {
                    cityAndCountry.append(item)
                }
            }
            
        } catch  {
            print("Error reading CSV file")
        }
    }

 

์•„๋ž˜์™€ ๊ฐ™์ด loadLocationsFromCSV ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 

Bundle.main.path(forResource:์—‘์…€ํŒŒ์ผ์ด๋ฆ„,ofType:์—‘์…€ํŒŒ์ผ๋ช…)์œผ๋กœ path๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

parseCSVAt์— ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋งŒ๋“ค์–ด์ค€ path๋ฅผ URL๋กœ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 private func loadLocationsFromCSV() {
        let path = Bundle.main.path(forResource: "location", ofType: "csv")!
        parseCSVAt(url: URL(fileURLWithPath: path))
        tableView.reloadData()
    }

 

์œ„์—์„œ ๋งŒ๋“ค์–ด์ค€ loadLocationFromCSV() ํ•จ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ viewDidLoad() ์•ˆ์— ์‹คํ–‰์‹œ์ผœ์ฃผ์…” ํ•ฉ๋‹ˆ๋‹ค!

  override func viewDidLoad() {
        super.viewDidLoad()
        loadLocationsFromCSV()
    }

๋งˆ์ง€๋ง‰์œผ๋กœ tableView์˜ dataSource๋ฅผ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

๋จผ์ € numberOfRowsInsection์—” cityAndCountry์˜ ์ˆซ์ž๋งŒํผ return ํ•ด์ฃผ์‹œ๊ณ 

 

cell์€ withIdentifier์— ์•„๊นŒ ๋งŒ๋“ค์–ด์ฃผ์‹  Identifier ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ์‹œ๊ณ  ์œ„์—์„œ ๋งŒ๋“ค์–ด์ค€ ExcelTableViewCell๋กœ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

cell.textLable์€ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ๋ณธ Title์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ cityAndCountry์˜ indextPath.row๋ฒˆ์งธ์˜ 0๋ฒˆ์งธ๋กœ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

 

cell.detatilTextLable์€ ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ๋ณธ Subtitle์ž…๋‹ˆ๋‹ค. ์ด๊ฑด cityAndCountry์˜ indextPath.row๋ฒˆ์งธ์˜ 1๋ฒˆ์งธ๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”!

 

์ด์œ ๋Š” ,๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์—ˆ๊ธฐ ๋•Œ๋ฌธ์— cityAndCountry๋Š” [["๋„์‹œ"],["๋‚˜๋ผ์ด๋ฆ„"]] ๊ณผ ๊ฐ™์ด ๋‹ด๊ฒจ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ cell์„ return ํ•ด์ค๋‹ˆ๋‹ค.

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return cityAndCountry.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ExcelTableViewCell") as! ExcelTableViewCell
        cell.textLabel?.text = cityAndCountry[indexPath.row][0]
        cell.detailTextLabel?.text = cityAndCountry[indexPath.row][1]
        return cell
    }

GIF

 

์ด๋ ‡๊ฒŒ ํ•œ ๋’ค ์‹คํ–‰์‹œ์ผœ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์—‘์…€ ์•ˆ์— ๋‹ด๊ฒจ์žˆ๋Š” ๋„์‹œ์™€ ๋‚˜๋ผ ์ด๋ฆ„์ด ํ…Œ์ด๋ธ”๋ทฐ ํ™”๋ฉด์— ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!!

 

 


Souce Code

 


์˜ค๋Š˜์€ ์ด๋ ‡๊ฒŒ Excel ํŒŒ์ผ ์•ˆ์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ํŒŒ์‹ฑํ•˜๊ณ  ํ…Œ์ด๋ธ”๋ทฐ์— ๋„์›Œ๋ณด๋Š” ๊ฒƒ์„ ์•Œ์•„๋ณด์•˜๋Š”๋ฐ์š”.

 

ํ˜น์‹œ ํ‹€๋ฆฐ์ ์ด ์žˆ๊ฑฐ๋‚˜ ์ง€์ ํ•˜์‹ค ๋ถ€๋ถ„ ํ˜น์€ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”~~

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€