본문 바로가기
STUDIES/IOS

테이블 뷰 관련 (마무리 필요)

by 두퍼 2022. 11. 2.

스토리보드에서 table view와 하나의 셀 배치하기

- 뷰컨트롤러 파일에 테이블 뷰 드래그 하여 인터페이스 요소 참조해주기

- 테이블 뷰  셀에 identifier 정해주기 "myCell"

 

셀에 해당하는 cocoa touch class (xib같이 만들어 줄 필요 없음) 파일 만들어주기

- "myCell" 에다가  해당 파일의 클래스 이름과 연결해주기

- 만들어진 파일 내 코드 지워주고 셀 내의 옵젝트들을 파일에 드래그 인터페이스 요소 참조 해주기

 

 

다시 view Controller로 돌아와서 실제로 데이터를 쌓고 렌더링 하는 것

- 배열을 생성

//데이터를 위한 객체를 생성
struct Sunset {
	let imageName: String
    let title: String
}

let data: [Sunset] = [
	Sunset(title:"item1", imageName: "item1"),
    Sunset(title:"item2", imageName: "item2"),
	Sunset(title:"item3", imageName: "item3"),
	Sunset(title:"item4", imageName: "item4"),
]

여기서 data는 실제로 테이블 뷰가 호출한 것임을 밝혀야 해서 데이터 소스를 self할당해줘야함

- 맨위에 class ViewController: UIViewController, UITableViewDataSource 추가!! 그러면 오류가 뜨고

- 그것을 viewDidLoad밑에 필수 기능을 추가

numberOfRowsInSection >> 자동완성

cellForRowAt >>자동완성

- viewDidload에는 table.dataSource = self

 

 

we want fixed height

- 맨위에 class ViewController: UIViewController, UITableViewDataSource, UITabelViewDelegate 추가

- viewDidload 안에는 table.dataSource = self 추가

- viewDidload 밑에

heightforrow (자동완성 ) at indexrow

return 140(원하는height따라)