Storyboard에서 TableView 사용
7579 단어 UITableViewSwiftiOS
고민하다
UITableView 사용할 때 종아리 발 덕분에Delegate 구현과 UItable View Controller 등항상 아래의 일을 생각하고 있다.
나는 실시하고 싶다
UITableViewDataSource, 만지고 싶다UITableView와 UITableViewController가 아니다.UITableViewDataSource의 코드만 쓴다.UITableViewDataSource를UIViewController에서 분리한 결과 init 중 dataSource 생성 실례의 맞춤형 클래스UIViewController에 불과했다.절차.
프로젝트 작성
TableView 구성
열기
Main.storyboard, 기본값ViewController에서 구성TableView
TableViewCell 구성
TableView에 TableViewCell를 추가합니다.Identifier에 설정cell
UItable ViewDataSource 구현
사용자 정의 클래스
UITableViewDataSource를 구현한 NSObject 만들기
import UIKit
class DataSource: NSObject, UITableViewDataSource {
let sections = [8, 9, 10]
let rows = [0, 15, 30, 45]
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let hour = sections[section]
return String(format: "%02d:00〜", hour)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let hour = sections[indexPath.section]
let minute = rows[indexPath.row]
cell.textLabel?.text = String(format: "%02d:%02d", hour, minute)
return cell
}
}
Storyboard에 데이터 소스 추가
Object 에 추가 ViewController
그리고
Object의 맞춤형 클래스에 DataSource를 지정한다.
TableView 및 DataSource 연결
마우스 오른쪽 버튼
TableView을 클릭하여 메뉴를 열고 dataSource의 Outlet 연결DataSource 객체를 엽니다.
실행
구축 후 확인 동작이 완성됩니다.
UITableView의 맞춤형 클래스가 필요하지 않습니다.UITableViewDelegete도 같은 요령에 따라 실시할 수 있다.
Reference
이 문제에 관하여(Storyboard에서 TableView 사용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/yajamon/items/be7a3f8d62895cda7cae텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)