This question already has an answer here:
UIView - How to get notified when the view is loaded?
(1 answer)
Closed 5 years ago.
I am currently creating a custom view. This view has a UITableView with it. The delegate for this control will be the backing class for the custom control.
class MyView: UIView {
#IBOutlet weak var autoCompleteView: UITableView!
}
extension MyView: UITableViewDelegate {
}
extension MyView: UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath as IndexPath)
cell.textLabel!.text = "\(indexPath.row) - Its working"
return cell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
}
Now at some point here, in this class, I need to get the delegate for the UITableView but that can only happen once the view has been loaded (otherwise autoCompleteView will be nil).
If it were in a UIViewController I could simply add it to viewDidLoad but I dont have that open here. So how can I go about setting the delegate for the UITableView
Now at some point here, in this class, I need to get the delegate for the UITableView but that can only happen once the view has been loaded
I am not entirely sure what you mean by that. The view MyView is loaded if you are inside a method of it. If the class would not be there, it could not be calling a method of it. So if you configure things in initWithFrame: everything should be fine.
The only thing I can think of otherwise is if your view is layed out in Interface Builder and thus will be decoded. In that case, while the view might already be loaded, some references might not be. For this, you will get a call to awakeFromNib after all decoding has completed.
//you can try these
class MyView: UIView {
#IBOutlet weak var autoCompleteView: UITableView!
override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
}
}
Related
First of all, I'm not sure the name of it is "extension view", please look at the screenshot, I mean the view with the yellow background.
I have made a custom table view in this view, and I did all the configurations, the problem is, in the end, I have to add delegate and dataSource for it.
Let's call the table cell inside of the view "OptionTableView".
I wrote these lines the viewdidload of the view controller (the controller that is inside the iPhone frame in the screenshot),
override func viewDidLoad() {
super.viewDidLoad()
OptionTableView.delegate = self
OptionTableView.dataSource = self
}
but it crashes the app with this error:
Thread 1: signal SIGABRT
Because it's UiView, I can't make a new file with a type of UIControlView for it.
Could you help me? How I can add delegate and dataSource for this extension UIView or whatever is called, that let me to use tableview inside it?
Thank you so much for your help
I think you not add protocol of tableview. I write a code for this:-
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var table_view: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
table_view.dataSource = self
table_view.delegate = self
}
}
I would like to modify the height of the View in Xib Size Inspector flexibly in the code.
I also want to modify the Height Equals in Constraints in the UICollectionView in Xib.
Short Code :
class ShopImagesViewCell: UITableViewCell
{
#IBOutlet weak var imageCollectionView: UICollectionView!
override func awakeFromNib()
{
super.awakeFromNib()
// ......
}
}
extension ShopImagesViewCell
{
func setView(urls : [String], isZzip : Bool)
{
self.imageCollectionView.delegate = self
self.imageCollectionView.dataSource = self
self.imageCollectionView.regCells(cells:
["ShopImageCollectionViewCell","ShopNonImageCollectionViewCell"])
// ......
}
}
extension ShopImagesViewCell : UICollectionViewDelegateFlowLayout, UITableViewDelegate
{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return CGFloat(291) // It does not work.
}
}
When Xib is first loaded, it is executed only once to fit in size.
Modifying Xib height length in code and modifying the Height Equals of Constraintsd in UICollectionView in code.
Please tell me where I should use the code.
According to you pasted code, it seems that you placed a custom UITableViewCell into the nib. Assumed on that, you cannot set height directly without the help of UITableViewDelegate.
Where the UITableViewDelegate is implemented or you intended to implement add this delegate method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// you have the indexPath based on which you can set the height dynamically, based on the cell position.
// return your intended height here
}
Hope it helps.
I'm just starting with Swift and xCode, and currently messing around with UITableView, I can't manage to just write 'test' into a table.
I created a UITableViewController in the Storyboard, specified a custom class for it (my swift file below), filled in 'ClientCell' as a reuse identifier of the cell and the code is as follows:
class TableViewController: UITableViewController {
#IBOutlet var clientTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
clientTable.dataSource = self.dataSource;
clientTable.delegate = self;
}
func numberOfRowsInSection(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ClientCell", forIndexPath: indexPath) as! CustomTableViewCell
cell.tableLabel.text = "test"
return cell
}
}
In the storyboard, I added a 'UILabel' into the prototype cell, and created an outlet for it named 'tableLabel' in the CustomTableViewCell.swift.
I confused by all the side-things I've to consider when doing something as simple as this.
When I run it, the simulator just shows a table with a lot of horizontal lines, but nowhere it says 'test'.
You haven't used the numberOfRowsInSection method.....
Additionally you also need to set the table view's data source and delegate to be the class you are writing these methods in (in case you haven't).
At a bare minimum you need this:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // this should really be from your data source
}
If you create a UITableViewController subclass in Xcode the template will have commented-out versions of all the methods you likely will want to fill out.
I have a ViewController that calls (clicking on a button) another View using this function
#IBAction func btnSeeContact(sender: AnyObject) {
self.performSegueWithIdentifier("segueSeeContact", sender: self)
}
and my prototype cell is "linked" to a custom View Controller named ContactsTableViewCell that I have created and it implements:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactsTableViewCell
cell.txtName.text = "test"
cell.txtPhone.text = "1234567890"
return cell
}
When I run the project, the button calls the table, but there is no Cell on it, and I put a breakpoint on those tableView functions and they are not being reached.
What am I missing here that those functions are never being called?
I am adding a new answer since my previous answer was up voted, so I don't want to make massive edits that one, and is still a valid way to fix your issue.
The issue is you have your custom classes confused. In your screen shot you can see that the the Table View Controller is not set to a custom class, it just says Table View Controller. That is the object that needs to get a custom implementation of the UITableViewController class.
Instead you seem to be setting the cell's class to a custom class, and implementing the delegate methods there. You still need a custom class for the table view cell, but it should be a custom class of UITableViewCell.
So your cell class should look something like this:
import UIKit
class YourCustomTableViewCell: UITableViewCell {
#IBOutlet weak var yourLabel1: UILabel!
#IBOutlet weak var yourLabel2: UILabel!
}
You will be given an instance of this cell to configure in cellForIndexPath.
So your Table view controller class should be set to a class that looks like below. The YourTableViewController is were you want to implement all the delegate methods.
Note: if you are using a UITableViewController dragged out from the storyboard, it will already have the tableView, and delegate / data source stuff already wired up for you. You will also notice that you are overriding the delegate methods as the UITableViewController class has default implementations of these. If you are just using a normal view controller, then see my previous answer for more details on how to set that up.
import UIKit
class YourTableViewController: UITableViewController {
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
if let cell = cell as? YourCustomTableViewCell {
cell.yourLabel1.text = "some text"
cell.yourLabel2.text = "some other text"
}
return cell
}
}
As others have commented, you really need to provide a little more context.
Here are a few things that might be going wrong, providing more context would confirm or deny this guesses.
First you don't show the numberOfSectionsInTableView method.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
I think you would need to provide a value other than 0
Secondly, since I don't see override in front of what I am sure you are intending to be UITableViewDelegate methods function calls, that means your view controller is not a UITableViewController. This makes me wonder if you defined this view controller as conforming to the UITableViewDelegate protocol and if you set the table view outlet delegate to self. (or even wired up the UITableView to an outlet)
If you use a plain UIViewController to host a table view you need to do the following:
Wire up your UITableView to an outlet in your view controller
Declare the view controller as conforming to the UITableViewDeleagate (and maybe UITableViewDataSource) protocol
set the table view's outlet delegate (and maybe dataSource) properties to self (the view controller implementing the protocols)
Implement the required methods
So something like this:
class MyTableViewController: UIViewController, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("your PrototypeCell", forIndexPath: indexPath)
// Configure the cell...
return cell
}
}
Numerous tutorials I've been through say the only code I need to display the array I want it to is:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var chatListTableView: UITableView!
var friends = ["Anthony", "Antonio", "Andy"]
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "ChatListCell")
cell.textLabel.text = self.friends[indexPath.row]
return cell
}
}
However, when I run the app, the tableView is still blank. What am I doing wrong? I feel that I am missing something.
All I want to do is display the array in the tableView.
Check if the ViewController is the datasource and delegate of the tableview
As Aci says in his answer, you have to set the data source and delegate of the table view to your view controller. The easiest way to do that is in Interface Builder.