์๋ ํ์ธ์ Foma ์ ๋๋ค!
์ค๋์ ํ์์ ์์ ์ ์๋ ์ ๋ณด๋ฅผ ์ด๋ป๊ฒ ๊ด๋ฆฌํ ์ ์์๊น? ๋ฅผ ์๊ฐํ๋ค๊ฐ
์์ ์ ํ์ฑํ๋๋ฒ์ ๋ํด์ ์๊ฒ๋ผ์ ์ ๋ณด๋ฅผ ๊ณต์ ํ๋ ค๊ณ ํฉ๋๋ค!
๋ฐ๋ก ์์ํ ๊ฒ์~
Preview

Excel
๋จผ์ Excel ํ์ผ์ธ .csv ํ์ผ์ ์ค๋นํด์ฃผ์ธ์!
์ ๋ ๋ค์๊ณผ ๊ฐ์ด ๋๋ผ์ ๋์ ๊ทธ๋ฆฌ๊ณ ๋๋ผ์ฝ๋๊ฐ ์๋ ์์ ์ ๋ณด๋ฅผ ์ค๋นํ์ต๋๋ค.
(ํน์ ํ์ํ์ ๋ถ๋ค์ ์ํด ํ์ผ์ ์ฒจ๋ถํด๋๊ฒ ์ต๋๋ค!)

Xcode
์ด ํ์ผ์ Xcode ํ๋ก์ ํธ ์์ ๋ฃ์ด์ฃผ์ธ์!

๊ทธ๋ฌ๋ฉด ์์ ์์ ์๋ ์ ๋ณด๋ค์ด Xcode ์์์ ์๋์ ๊ฐ์ด ๋ณด์ด๊ฒ ๋ฉ๋๋ค.

Storyboard
๋๋ผ์ด๋ฆ๊ณผ ๋์๋ฅผ ๋์ธ ํ ์ด๋ธ๋ทฐ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค์ด์ค์๋ค!

ํ ์ด๋ธ๋ทฐ์ ์์ Style์ Subtitle๋ก ๋ฐ๊พธ๋ฉด

์๋์ ๊ฐ์ด ์๋์ผ๋ก Title๊ณผ Subtitle์ด ์๋ ์ ์ด ๋ง๋ค์ด์ง๊ฒ ๋ฉ๋๋ค.

๊ทธ๋ฆฌ๊ณค ExcelTableViewController์ ExcelTableViewCell์ ๋ง๋ค์ด์ฃผ์ธ์!



์๋์ ๊ฐ์ด ํ์ผ์ด ๋ง๋ค์ด์ก๋ค๋ฉด

๋ค์ ์คํ ๋ฆฌ๋ณด๋๋ก ์ด๋ํด์ TableViewController ํด๋์ค๋ฅผ ExcelTableViewController๋ก ๋ฐ๊ฟ์ค๋๋ค.

๊ทธ๋ฆฌ๊ณ TableViewCell์ ํด๋์ค๋ ExcelTableViewCell๋ก ๋ฐ๊ฟ์ฃผ์๊ณ

๋ฐ๋์ Identifier๋ ์ํ์๋ ์ด๋ฆ์ผ๋ก ์ค์ ํด์ฃผ์ธ์.(์ ๋ ๊ธฐ์ตํ๊ธฐ ์ฝ๊ฒ Class ์ด๋ฆ์ด๋ ๋๊ฐ์ด ํ์ต๋๋ค.)

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
import UIKit | |
class ExcelTableViewController: UITableViewController { | |
var cityAndCountry:[[String]] = [] | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
loadLocationsFromCSV() | |
} | |
private func loadLocationsFromCSV() { | |
let path = Bundle.main.path(forResource: "location", ofType: "csv")! | |
parseCSVAt(url: URL(fileURLWithPath: path)) | |
tableView.reloadData() | |
} | |
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") | |
} | |
} | |
// MARK: - Table view data source | |
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 | |
} | |
} |
์ค๋์ ์ด๋ ๊ฒ Excel ํ์ผ ์์ ์๋ ์ ๋ณด๋ฅผ ํ์ฑํ๊ณ ํ ์ด๋ธ๋ทฐ์ ๋์๋ณด๋ ๊ฒ์ ์์๋ณด์๋๋ฐ์.
ํน์ ํ๋ฆฐ์ ์ด ์๊ฑฐ๋ ์ง์ ํ์ค ๋ถ๋ถ ํน์ ๊ถ๊ธํ์ ์ ์ด ์๋ค๋ฉด ์ธ์ ๋ ๋๊ธ๋ก ์๋ ค์ฃผ์ธ์~~
๋๊ธ