설정 화면을 코드만으로 쓸 때 항상 고민
4476 단어 Swift3.0
자주 있는 이런 설정 화면
보통 어쨌든 section과 row의 번호 지정・・・
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "ヘルプ"
} else if section == 1 {
return "設定"
}
}
적어도, define하고・・・ 게다가, 아니.
이런 식으로 만들어 보았습니다.
더 좋은 접근법이 있으면 알려주세요! !
import UIKit
class ViewController: UIViewController {
var tableView: UITableView!
// セクションの情報を定義
enum SectionMember: Int {
case help, about, last
var title: String? {
let titles = ["ヘルプ", "設定", nil]
return titles[self.rawValue]
}
var count: Int {
let members = [AccountSectionMember.last.rawValue, SettingSectionMember.last.rawValue, 0]
return members[self.rawValue]
}
}
// セクションのメンバー情報を定義
enum AccountSectionMember: Int {
case account, timeline, push, privacy, payment, last
var title: String? {
let titles = ["アカウント", "タイムライン", "通知", "プライバシー", "お支払", nil]
return titles[self.rawValue]
}
}
enum SettingSectionMember: Int {
case sound, data, about, last
var title: String? {
let titles = ["サウンド", "データ利用の設定", "アプリについて", nil]
return titles[self.rawValue]
}
}
// テーブルの情報をさくっととれるように
struct TableInfo {
var section: SectionMember
var row: Int = 0
init(inSection section: Int) {
self.section = SectionMember(rawValue: section)!
}
init(indexPath: IndexPath) {
self.init(inSection: indexPath.section)
self.row = indexPath.row
}
func rowMemberTitle() -> String? {
switch self.section {
case .help:
return AccountSectionMember(rawValue: row)?.title
case .about:
return SettingSectionMember(rawValue: row)?.title
default:
return nil
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
tableView = UITableView(frame: self.view.frame, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.separatorStyle = .singleLine
self.view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return SectionMember.last.rawValue
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 35
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableInfo(inSection: section).section.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return TableInfo(inSection: section).section.title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath as IndexPath)
let tableInfo = TableInfo(indexPath: indexPath)
// タイトルの取得
cell.textLabel?.text = tableInfo.rowMemberTitle()!
// 個別の処理はこんな感じ
if tableInfo.section == .help && tableInfo.row == AccountSectionMember.account.rawValue {
cell.accessoryType = .disclosureIndicator
}
return cell
}
}
Reference
이 문제에 관하여(설정 화면을 코드만으로 쓸 때 항상 고민), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/rika-tawashi/items/6461180c518afeeb20fb
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "ヘルプ"
} else if section == 1 {
return "設定"
}
}
적어도, define하고・・・ 게다가, 아니.
이런 식으로 만들어 보았습니다.
더 좋은 접근법이 있으면 알려주세요! !
import UIKit
class ViewController: UIViewController {
var tableView: UITableView!
// セクションの情報を定義
enum SectionMember: Int {
case help, about, last
var title: String? {
let titles = ["ヘルプ", "設定", nil]
return titles[self.rawValue]
}
var count: Int {
let members = [AccountSectionMember.last.rawValue, SettingSectionMember.last.rawValue, 0]
return members[self.rawValue]
}
}
// セクションのメンバー情報を定義
enum AccountSectionMember: Int {
case account, timeline, push, privacy, payment, last
var title: String? {
let titles = ["アカウント", "タイムライン", "通知", "プライバシー", "お支払", nil]
return titles[self.rawValue]
}
}
enum SettingSectionMember: Int {
case sound, data, about, last
var title: String? {
let titles = ["サウンド", "データ利用の設定", "アプリについて", nil]
return titles[self.rawValue]
}
}
// テーブルの情報をさくっととれるように
struct TableInfo {
var section: SectionMember
var row: Int = 0
init(inSection section: Int) {
self.section = SectionMember(rawValue: section)!
}
init(indexPath: IndexPath) {
self.init(inSection: indexPath.section)
self.row = indexPath.row
}
func rowMemberTitle() -> String? {
switch self.section {
case .help:
return AccountSectionMember(rawValue: row)?.title
case .about:
return SettingSectionMember(rawValue: row)?.title
default:
return nil
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
tableView = UITableView(frame: self.view.frame, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.separatorStyle = .singleLine
self.view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return SectionMember.last.rawValue
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 35
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableInfo(inSection: section).section.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return TableInfo(inSection: section).section.title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath as IndexPath)
let tableInfo = TableInfo(indexPath: indexPath)
// タイトルの取得
cell.textLabel?.text = tableInfo.rowMemberTitle()!
// 個別の処理はこんな感じ
if tableInfo.section == .help && tableInfo.row == AccountSectionMember.account.rawValue {
cell.accessoryType = .disclosureIndicator
}
return cell
}
}
Reference
이 문제에 관하여(설정 화면을 코드만으로 쓸 때 항상 고민), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/rika-tawashi/items/6461180c518afeeb20fb
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import UIKit
class ViewController: UIViewController {
var tableView: UITableView!
// セクションの情報を定義
enum SectionMember: Int {
case help, about, last
var title: String? {
let titles = ["ヘルプ", "設定", nil]
return titles[self.rawValue]
}
var count: Int {
let members = [AccountSectionMember.last.rawValue, SettingSectionMember.last.rawValue, 0]
return members[self.rawValue]
}
}
// セクションのメンバー情報を定義
enum AccountSectionMember: Int {
case account, timeline, push, privacy, payment, last
var title: String? {
let titles = ["アカウント", "タイムライン", "通知", "プライバシー", "お支払", nil]
return titles[self.rawValue]
}
}
enum SettingSectionMember: Int {
case sound, data, about, last
var title: String? {
let titles = ["サウンド", "データ利用の設定", "アプリについて", nil]
return titles[self.rawValue]
}
}
// テーブルの情報をさくっととれるように
struct TableInfo {
var section: SectionMember
var row: Int = 0
init(inSection section: Int) {
self.section = SectionMember(rawValue: section)!
}
init(indexPath: IndexPath) {
self.init(inSection: indexPath.section)
self.row = indexPath.row
}
func rowMemberTitle() -> String? {
switch self.section {
case .help:
return AccountSectionMember(rawValue: row)?.title
case .about:
return SettingSectionMember(rawValue: row)?.title
default:
return nil
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
tableView = UITableView(frame: self.view.frame, style: .grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.separatorStyle = .singleLine
self.view.addSubview(tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return SectionMember.last.rawValue
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 35
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableInfo(inSection: section).section.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return TableInfo(inSection: section).section.title
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath as IndexPath)
let tableInfo = TableInfo(indexPath: indexPath)
// タイトルの取得
cell.textLabel?.text = tableInfo.rowMemberTitle()!
// 個別の処理はこんな感じ
if tableInfo.section == .help && tableInfo.row == AccountSectionMember.account.rawValue {
cell.accessoryType = .disclosureIndicator
}
return cell
}
}
Reference
이 문제에 관하여(설정 화면을 코드만으로 쓸 때 항상 고민), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/rika-tawashi/items/6461180c518afeeb20fb텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)