user12388503
user12388503

Reputation:

Swift - Subclassing UITableView

in my project I have a class WhishlistTableViewController which is a subclass of UITableViewController. I am struggling to set its properties. Why don't I have access to UITableViewController functions in my WhishlistTableViewController?

This is what I tried but it just gives me "Value of type 'WhishlistTableViewController' has no member 'layer' " error.

Setup inside a UIViewController:

let theTableView: WhishlistTableViewController = {
   let v = WhishlistTableViewController()        // -> error
    v.layer.masksToBounds = true                 // -> error
    v.layer.borderColor = UIColor.white.cgColor  // -> error
    v.layer.borderWidth = 2.0                    // -> error
    v.translatesAutoresizingMaskIntoConstraints = false // -> error
    return v
}()

WhishlistTableViewController:

class WhishlistTableViewController: UITableViewController {

public var wishList : [Wish]?

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.register(WhishCell.self, forCellReuseIdentifier: WhishCell.reuseID)
}

// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return wishList?.count ?? 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath)
    let currentWish = self.wishList![indexPath.row]
    cell.textLabel?.text = currentWish.wishName
    return cell
}

}

class Wish: NSObject {
    public var wishName : String?
    init(withWishName name: String) {
        super.init()
        wishName = name
    }
}

I am grateful for every help :)

Upvotes: 0

Views: 48

Answers (1)

Dixit Rathod
Dixit Rathod

Reputation: 177

Actually your WhishlistTableViewController is not a UIView so you can't access direct it's layer. please try below

let theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()        
 v.view.layer.masksToBounds = true                 
 v.view.layer.borderColor = UIColor.white.cgColor  
 v.view.layer.borderWidth = 2.0                    
 v.view.translatesAutoresizingMaskIntoConstraints = false 
return v
}()

Upvotes: 1

Related Questions