swift tableview custom 스위프트 테이블뷰 커스텀 



//

//  MainViewController.swift

//  TableViewAI

//

//  Created by stayfoolish on 20/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class MainViewController: UIViewController, UITableViewDataSource {


    @IBOutlet var tableView: UITableView!

    

    let sampleData = SampleData()

    

    override func viewDidLoad() {

        super.viewDidLoad()


        // Do any additional setup after loading the view.

        self.tableView.dataSource = self

        self.tableView.tableFooterView = UIView(frame: .zero)

    }

    

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

        return self.sampleData.samples.count

    }

    

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

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

        

    let sample = self.sampleData.samples[indexPath.row]

        

        cell.titleLabel.text = sample.title

        cell.descriptionLabel.text = sample.description

        cell.imageView?.image = UIImage(named: sample.image)

        return cell

    }



}


//

//  SampleData.swift

//  TableViewAI

//

//  Created by stayfoolish on 20/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import Foundation


struct Sample {

    let title: String

    let description: String

    let image: String

}


struct SampleData {

    let samples = [

        Sample(title: "Photo Object Detection", description: "불러온 이미지에 사물 인식", image: "ic_photo"),

        Sample(title: "Real Time Object Detection", description: "실시간으로 카메라에 보이는 사물 인식", image: "ic_camera"),

        Sample(title: "Facial Analysis", description: "사람 얼굴로부터 나이, 성별, 감정 추측", image: "ic_emotion")

    ]

}


//

//  MainFeatureCell.swift

//  TableViewAI

//

//  Created by stayfoolish on 20/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class MainFeatureCell: UITableViewCell {


    @IBOutlet var featureImageView: UIImageView!

    @IBOutlet var titleLabel: UILabel!

    @IBOutlet var descriptionLabel: UILabel!

   

}


swift tableview array 스위프트 테이블뷰 







//

//  MainViewController.swift

//  TableViewAI

//

//  Created by stayfoolish on 20/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class MainViewController: UIViewController, UITableViewDataSource {


    @IBOutlet var tableView: UITableView!

    

    let dataArray = ["1","2","3","4","5","6","7","8","9","10"]

    

    override func viewDidLoad() {

        super.viewDidLoad()


        // Do any additional setup after loading the view.

        self.tableView.dataSource = self

    }

    

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

        return dataArray.count

    }

    

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "numberCell", for: indexPath)

        

        cell.textLabel?.text = dataArray[indexPath.row]

        

        return cell

    }



}


swift tableview 간단한 스위프트 테이블뷰 , 디테일 뷰 



//

//  ViewController.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class ViewController: UIViewController {

    

    @IBOutlet var tableView : UITableView!

    let dataSource: TableData = TableData()

    let delegate: TableDelegate = TableDelegate()

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        self.tableView.dataSource = self.dataSource

        self.tableView.delegate = self.delegate

        

    }


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        guard let cell: UITableViewCell = sender as? UITableViewCell else {

            return

        }

        

        guard let index: IndexPath = self.tableView.indexPath(for: cell) else {

            return

        }

        

        guard let nextViewController: DetailViewController = segue.destination as? DetailViewController else {

            return

        }

        

        nextViewController.index = index

    }


}



//

//  TableData.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class TableData: NSObject, UITableViewDataSource {

    

    // 섹션수

    func numberOfSections(in tableView: UITableView) -> Int {

        return 3

    }

    

    // 섹션별 로우수

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

        

        if section == 0 {

            return 1

        }

        return 15

    }

    

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

        let cell: UITableViewCell

        cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

        cell.textLabel?.text = "\(indexPath.section) - \(indexPath.row)"

        

        return cell

    }


}


//

//  TableDelegate.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class TableDelegate: NSObject, UITableViewDelegate {

    

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

        print("did select \(indexPath)")

    }


}



//

//  DetailViewController.swift

//  SimpleTable

//

//  Created by stayfoolish on 15/11/2018.

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class DetailViewController: UIViewController {


    @IBOutlet var label: UILabel!

    var index: IndexPath!

    

    

    override func viewDidLoad() {

        super.viewDidLoad()


        // Do any additional setup after loading the view.

        self.label.text = "\(self.index.description)"

    }

    


    /*

    // MARK: - Navigation


    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destination.

        // Pass the selected object to the new view controller.

    }

    */


}












+ Recent posts