swift tableview customcell detail 스위프트 테이블뷰 커스텀셀 디테일 뷰 




//

//  MasterViewController.swift

//  CustomCell

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class MasterViewController: UITableViewController {

    

    var itemList = [[String:String]]()

    

    


    override func viewDidLoad() {

        super.viewDidLoad()

        let item1 = ["name":"사과","image":"apple.jpeg","amount":"6","value":"3000원"]

        let item2=["name":"블루베리","image":"blueberry.jpg","amount":"10","value":"30000원"]

        let item3=["name":"당근","image":"carrot.jpg","amount":"13","value":"5000원"]

        let item4=["name":"체리","image":"cherry.png","amount":"1","value":"2000원"]

        let item5=["name":"포도","image":"grape.jpg","amount":"13","value":"1000원"]

        let item6=["name":"키위","image":"kiwi.png","amount":"2","value":"15000원"]

        let item7=["name":"레몬","image":"lemon.png","amount":"3","value":"6000원"]

        let item8=["name":"라임","image":"lime.jpg","amount":"4","value":"4000원"]

        let item9=["name":"고기","image":"meat.jpg","amount":"5","value":"2000원"]

        let item10=["name":"딸기","image":"strawberry.jpg","amount":"7","value":"8000원"]

        let item11=["name":"토마토","image":"tomato.png","amount":"30","value":"3000원"]

        let item12=["name":"야채","image":"vegetable.jpg","amount":"40","value":"7000원"]

        let item13=["name":"멜론","image":"watermelon.png","amount":"5","value":"10000원"]

        

        itemList = [item1, item2,item3,item4,item5,item6,item7,item8,item9,item10,item11,item12,item13]

        

        let backgroundImageView = UIImageView(image: UIImage(named: "background.jpg"))

        backgroundImageView.contentMode = UIView.ContentMode.scaleAspectFill

        backgroundImageView.frame = self.tableView.frame

        

        self.tableView.backgroundView = backgroundImageView

    }


    // MARK: - Table view data source

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

        // #warning Incomplete implementation, return the number of rows

        return itemList.count

    }


    

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

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

        

        cell.backgroundColor = UIColor.clear


        let dictTemp = itemList[indexPath.row]

        

        cell.nameLabel.text = dictTemp["name"]

        cell.amountLabel.text = dictTemp["amount"]

        cell.valueLabel.text = dictTemp["value"]

        

        cell.imgView.image = UIImage(named: dictTemp["image"]!)

        

        

        return cell

    }

    

    // 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.

        if segue.identifier == "showDetail" {

            (segue.destination as! DetailViewController).detailData = itemList[(self.tableView.indexPathForSelectedRow)!.row]

        }

        

    }

    


    /*

    // Override to support conditional editing of the table view.

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

        // Return false if you do not want the specified item to be editable.

        return true

    }

    */


    /*

    // Override to support editing the table view.

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {

            // Delete the row from the data source

            tableView.deleteRows(at: [indexPath], with: .fade)

        } else if editingStyle == .insert {

            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

        }    

    }

    */


    /*

    // Override to support rearranging the table view.

    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {


    }

    */


    /*

    // Override to support conditional rearranging of the table view.

    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {

        // Return false if you do not want the item to be re-orderable.

        return true

    }

    */



    


}


//

//  DetailViewController.swift

//  CustomCell

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class DetailViewController: UIViewController {

    @IBOutlet var imgView: UIImageView!

    @IBOutlet var nameLabel: UILabel!

    @IBOutlet var amountLabel: UILabel!

    @IBOutlet var valueLabel: UILabel!

    

    

    

    var detailData = [String:String]()


    override func viewDidLoad() {

        super.viewDidLoad()

//        print(detailData)

        imgView.layer.cornerRadius = 50.0

        imgView.layer.masksToBounds = true

        

        

        

        nameLabel.text = detailData["name"]

        amountLabel.text = detailData["amount"]

        valueLabel.text = detailData["value"]

        imgView.image = UIImage(named: detailData["image"]!)

        

        

        // Do any additional setup after loading the view.

    }

    


    /*

    // 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.

    }

    */


}


//

//  CustomCell.swift

//  CustomCell

//

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

//  Copyright © 2018 stayfoolish. All rights reserved.

//


import UIKit


class CustomCell: UITableViewCell {


    @IBOutlet var nameLabel: UILabel!

    @IBOutlet var amountLabel: UILabel!

    @IBOutlet var valueLabel: UILabel!

    @IBOutlet var imgView: UIImageView!

    

    override func awakeFromNib() {

        super.awakeFromNib()

        // Initialization code

        imgView.contentMode = UIView.ContentMode.scaleAspectFill

        imgView.layer.cornerRadius = 50.0

        imgView.layer.masksToBounds = true

    }


    override func setSelected(_ selected: Bool, animated: Bool) {

        super.setSelected(selected, animated: animated)


        // Configure the view for the selected state

    }


}











+ Recent posts