본문 바로가기
ios 뽀개기/ios 응용해보기

유동적인 테이블 뷰 1

by 인생여희 2017. 12. 12.
반응형

유동적인 테이블 뷰 1


테이블 추가 좌우위아래 제약조건 0000

테이블 추가

라벨 추가 모든 간격 20, 왼쪽 정렬, 줄수는 가변적인 줄수 0

화면두개 나누기

코드 작업{

테이블 클래스 지정

테이블 -> custom class에서 만든 클래스 연결

라벨 오른쪽드래그 아웃렛 변수 지정

부모 클래스 상속 2

필수로 구현해야 하는 함수 구현해주기

스토리보드 - 클릭 -> identifier 이름 지정


}

테이블뷰 델리게이트 데이타 소스 오른쪽 클릭으로 

뷰컨트롤러에 연결해주기



사진 참고


테이블 아웃렛 변수 지정해주기

유동적인 테이블 크기 지정해주기


사진참고


클릭했을 true 되게해서 크게 해주기


//

//  ViewController.swift

//  TableView

//

//  Created by MacBookPro on 2017. 12. 12..

//  Copyright © 2017년 MacBookPro. All rights reserved.

//


import UIKit


class MyCell: UITableViewCell {

    @IBOutlet weak var myLabel: UILabel!

    

}



class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {


    var heightArray: NSMutableArray = []

    @IBOutlet weak var myTableView: UITableView!

    var textDataArray: NSArray = ["sort text",

                                  "long long long long text",

                                  "very long long long long long very long long long long long long long long long long long very long long long long long long long long text"]

    override func viewDidLoad() {

        super.viewDidLoad()

        

        myTableView.rowHeight = UITableViewAutomaticDimension //높이를 유동적으로

        for _ in 0...14 {

            heightArray.add(false)

        }

    }


    

    //높이를 유동적으로

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension

    }

    

    //셀 개수 리턴

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 15

    }

    

    //테이블 구조 설정

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        //스토리보드에서 만든 셀 객체 가져오기

        let cell = tableView.dequeueReusableCell(withIdentifier: "expendCell", for: indexPath) as! MyCell

        //배열에 있는 값을 테이블 레이아웃에 있는 라벨에 할당해서 리턴

        cell.myLabel.text = textDataArray[indexPath.row % textDataArray.count] as? String

        

        if heightArray[indexPath.row] as! Bool == false {

            cell.myLabel.numberOfLines = 1

        }else{

            cell.myLabel.numberOfLines = 0

        }

        

        return cell

    

    }

//테이블 클릭했을 때

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if heightArray[indexPath.row] as! Bool == false {

            heightArray.replaceObject(at: indexPath.row, with: true)

        }else{

            heightArray.replaceObject(at: indexPath.row, with: false)

        }

        tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)

    }

    

}



반응형

댓글