3
\$\begingroup\$

I was told the function "hompage" was too clunky to conveniently make a tableview. The cells will have the information in "PHOTO ALBUM ICONS" and "BUTTON ON TOP OF PHOTO ALBUM ICONS".

import UIKit
import Cartography
import ChameleonFramework

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

    //DECLORATION OF TABLEVIEW

    var albumTable = UITableView()

    //DECLORATION OF TITLE

    var catalogs = UILabel()


    //DECLORATION OF ALBUM BUTTON ICONS

    var button: UIImage = UIImage (named: "AlbumButton")!

    var album1:UIImageView!
    var album2:UIImageView!
    var album3:UIImageView!
    var album4:UIImageView!

    //DECLORATION OF BUTTONS ON TOP OF ICON

    var button1 = UIButton()
    var button2 = UIButton()
    var button3 = UIButton()
    var button4 = UIButton()

    //DECLORATION OF CAMERA BUTTON ICON

    var camLabel = UILabel() //SNAP IT!
    var camera: UIImage = UIImage (named: "Camera")!
    var camera1:UIImageView!
    var button5 = UIButton()//CLEAR BUTTON COVERING CAMERA ICON AND LABEL


    override func viewDidLoad() {
        super.viewDidLoad()

        homePage()

        //TABLEVIEW

        albumTable.delegate  =  self
        albumTable.dataSource  = self
        albumTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //FUNCTION FOR MAINPAGE VIEWS

    func homePage() {

        //BACKGROUND COLOR

        self.view.backgroundColor = UIColor.flatPowderBlueColor()

        //TITLE

        self.view.addSubview(catalogs)
        self.catalogs.text = "MY ALBUMS"
        self.catalogs.font = UIFont(name: "ChalkboardSE-Bold", size: 28)
            self.catalogs.textAlignment = .Center}

        //PHOTO ALBUM ICONS

        album1 = UIImageView()
        album1.contentMode = .ScaleAspectFill
        album2 = UIImageView()
        album2.contentMode = .ScaleAspectFill
        album3 = UIImageView()
        album3.contentMode = .ScaleAspectFill
        album4 = UIImageView()
        album4.contentMode = .ScaleAspectFill

        album1.image = button
        album2.image = button
        album3.image = button
        album4.image = button

        /*album1.backgroundColor = UIColor.redColor()
        album2.backgroundColor = UIColor.redColor()
        album3.backgroundColor = UIColor.redColor()
        album4.backgroundColor = UIColor.redColor()*/

        self.view.addSubview(album1)
        self.view.addSubview(album2)
        self.view.addSubview(album3)
        self.view.addSubview(album4)

        //BUTTON ON TOP OF PHOTO ALBUM ICONS


        self.button1.setTitle("BUGS", forState: UIControlState.Normal)
        //button1.backgroundColor = UIColor.whiteColor()

        self.view.addSubview(button1)

        self.button2.setTitle("COOKING", forState: UIControlState.Normal)
        //button2.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(button2)

        self.button3.setTitle("CARS", forState: UIControlState.Normal)
        //button3.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(button3)

        self.button4.setTitle("BIRDS", forState: UIControlState.Normal)
        //button4.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(button4)

        // CAMERA ICON


        camera1 = UIImageView()
        camera1.image = camera
        camera1.contentMode = .ScaleAspectFill
        self.view.addSubview(camera1)
        //camera1.backgroundColor = UIColor.redColor()

        //CAMERA BUTTON

        self.button5.setTitle("", forState: UIControlState.Normal)
        //button5.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(button5)

        //CAMERA LABEL

        self.camLabel.text = "SNAP IT!"
        self.camLabel.font = UIFont(name: "ChalkboardSE-Regular", size: 16)
        self.camLabel.textAlignment = .Center
        self.view.addSubview(camLabel)



        //ARRAY FOR ALL VIEWS ON 1ST PAGE

        let views:[UIView] = [catalogs, album1, album2, album3, album4, button1, button2, button3, button4, camera1, button5, camLabel]

        //CONSTRAINTS FOR ALL VIEWS ON FIRST PAGE

        constrain(views) { (_views) in

            //HEADING CONTRAITS

            _views[0].height == 50
            _views[0].width == 210
            _views[0].top == (_views[0].superview?.top)! + 20
            _views[0].centerX == (_views[0].superview?.centerX)!

            //ALBUM IMAGEVIEW CONSTRAINTS

            _views[1].height == 180
            _views[1].width == 180
            _views[1].top == (_views[1].superview?.top)! + 55
            _views[1].left == (_views[1].superview?.left)! + 0

            _views[2].height == 180
            _views[2].width == 180
            _views[2].top == (_views[2].superview?.top)! + 55
            _views[2].right == (_views[2].superview?.right)! + 0

            _views[3].height == 180
            _views[3].width == 180
            _views[3].top == (_views[1].bottom) + 0
            _views[3].left == (_views[3].superview?.left)! + 0

            _views[4].height == 180
            _views[4].width == 180
            _views[4].top == (_views[2].bottom) + 0
            _views[4].right == (_views[4].superview?.right)! + 0

            //BUTTONS ON TOP OF IMGAGEVIEW CONSTRAINTS

            _views[5].height == 60
            _views[5].width == 82
            _views[5].top == (_views[1].top) + 50
            _views[5].left == (_views[5].superview?.left)! + 53.2

            _views[6].height == 60
            _views[6].width == 82
            _views[6].top == (_views[2].top) + 50
            _views[6].right == (_views[5].superview?.right)! - 46.5

            _views[7].height == 60
            _views[7].width == 82
            _views[7].top == (_views[1].bottom) + 50
            _views[7].left == (_views[5].superview?.left)! + 53.2

            _views[8].height == 60
            _views[8].width == 82
            _views[8].top == (_views[2].bottom) + 50
            _views[8].right == (_views[5].superview?.right)! - 46.5

            //CAMERA ICON CONSTRAINTS

            _views[9].height == 100
            _views[9].width == 130
            _views[9].bottom == (_views[9].superview?.bottom)! - 60
            _views[9].centerX == (_views[9].superview?.centerX)!

            //CAMERA BUTTON CONSTRAINTS

            _views[10].height == 150
            _views[10].width == 130
            _views[10].bottom == (_views[9].superview?.bottom)! - 20
            _views[10].centerX == (_views[9].superview?.centerX)!

            //CAMERA LABEL CONSTRAINTS

            _views[11].height == 20
            _views[11].width == 80
            _views[11].bottom == (_views[11].superview?.bottom)! - 47
            _views[11].centerX == (_views[11].superview?.centerX)!


        }


    }
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

A few things I noticed:

  1. You have: var button1 = UIButton() and so on in some places, but var x: Type in others.

    It seems to me better to just do var button1: UIButton. This way, the compiler will yell at you if you forget to instantiate / assign it. (It's basically free error checking).

    Obviously if you have a default value you want to assign, then do it. But here, you are just instantiating an empty UIButton--that will only crash your program if you forget to assign it.

  2. You call homePage() before it's defined... I know that this is technically OK, but it causes you to jump around in the code when trying to read it from top-to-bottom. This can subconsciously make your program more difficult to understand / maintain, since most people learn to program "procedurally".

    I think most people don't do this stringently, but to me, at least, I'm like "WTF is homePage()?" Then I have to scroll down to see it, then scroll back up, while trying to keep everything sorted in my head.

  3. Some of your spacing is a bit inconsistent.. I know it can be a pain (especially in Xcode). But at times you are ending a block with one empty line, then a comment, then two empty lines, and other times you end a block with two empty lines, a comment, then one empty line.

    Something like this could be better:

    // DO STUFF
    
    math().do().moreMath()
    
    // DO MORE MATH
    
    math().do().moreMath()
    

    Or:

    // Mathtime!:
    
    math().do()
    math().do()
    
    
    // Recess time!
    
    recess().do()
    

There is also what appears to be a bug that I fixed˚:

func homePage() {

    //BACKGROUND COLOR

    self.view.backgroundColor = UIColor.flatPowderBlueColor()

    //TITLE

    self.view.addSubview(catalogs)
    self.catalogs.text = "MY ALBUMS"
    self.catalogs.font = UIFont(name: "ChalkboardSE-Bold", size: 28)
    self.catalogs.textAlignment = .Center

Fixing that requires you to put another } at the very bottom.

Also, I'm not 100% on this, as it appears to be a method in another API, constrain(views) { (_views) in but a simple block might be easier:

constrainViews: do {

    //HEADING CONSTRAITS

    views[0].height == 50

Hope this helps some!

\$\endgroup\$
1
  • \$\begingroup\$ I actually just made a collection view instead. It made it A LOT easier. Thanks \$\endgroup\$ Nov 7, 2016 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.