Big Boss
Big Boss

Reputation: 63

How to colour the view behind table view in xcode ios Swift?

I was making an ios app in xcode made a table view and added an image above it in the view controller and coloured both of them red both view and table view but when i drag down the table view till the end the view behind comes white instead of red. Can you tell me how to fix it Thnx in Advance;)

Code:

import UIKit
import Firebase
import FirebaseDatabase
import PINRemoteImage
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var ref:FIRDatabaseReference!
    var data = [DataModel]()
        @IBOutlet weak var one: UIImageView!



    @IBOutlet weak var mytable: UITableView!


    override func viewDidLoad() {


        doSetup()

        self.view.backgroundColor = UIColor(red: 138/255, green: 56/255, blue: 48/255, alpha: 1.0)

        mytable.dataSource = self
        mytable.delegate = self

        super.viewDidLoad()

    }

    func doSetup(){


        ref = FIRDatabase.database().reference()
        //ref.child("Posts").childByAutoId().setValue(["post_text":" Nami ","imageUrl" : ""])
        ref.child("Posts").observe(.childAdded, with: { (snapshot) in

            let newData = snapshot.value as! [String:String]

            let post_text = newData["post_text"]
            let imageUrl = newData["imageUrl"]

            let converted = DataModel(post_text: post_text, imageUrl: imageUrl)

            self.data.append(converted)

            self.mytable.reloadData()

            self.one.pin_setImage(from: URL(string : "https://otaku-w9pxf76zfsktmx3e.stackpathdns.com/wp-content/uploads/2016/06/Luffy.png"))


        })

    }




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


        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell

        let nth_data = data[indexPath.row]
        cell.posttext.text = nth_data.post_text
        cell.posttext.textColor = .white
        cell.myimage.pin_setImage(from: URL(string: nth_data.imageUrl!))
        cell.backgroundColor = UIColor(red: 138/255, green: 56/255, blue: 48/255, alpha: 1.0)
        return (cell)
    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }


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

}

Upvotes: 1

Views: 853

Answers (2)

Vadim Popov
Vadim Popov

Reputation: 1217

If you want red background below all the subviews, place that inside your UIViewController's viewDidLoad function:

self.view.backgroundColor = UIColor.red

UPD

In this case you can either set myTable.backgroundColor to be red, or set self.view.backgroundColor to red having myTable and each of the cells' colors set to UIColor.clear

Upvotes: 1

Lumialxk
Lumialxk

Reputation: 6369

When you run your project, use this to debug view. Believe it or not, it's very useful. enter image description here

Upvotes: 1

Related Questions