I have a UIViewController(in Objective C) which contains various subviews. Now I have added a new UIView(swift file and a xib file) as a subview to this view controller. In this UIView I have dragged and dropped UITableView and set datasource and delegate to file owner. Also, I have created a separate UITableViewCell class(in swift).
I want to add this UITableViewCell in UITableView(created in UIView). And add this entire view as a subview to UIViewController. The frames of the UIView are set in view controller
When I added this UIView as subview tables and cells are not displayed and tried various methods but either the app crashes by tableview as nil or the cell is not loaded at all and rows are not displayed
Please help me out!!!
Thank you
Code Snippet:
UIViewController
- (void)addNewView() {
NewUIView *view = [[NewUIView alloc] initWithFrame:CGRectMake(0, 200, SCREEN_WIDTH, self.view.bounds.size.height)];
[self.contentScrollView addSubview:view];
}
UIView(NewUIView)
Here I have added an outlet of UITableView with delegate methods of UITableView in extension(connected delegate and datasource from xib file to file owner).
And add cell
UITableViewCell
I want want to add this cell to UITableView In UIView.
I have a TableView and a CollectionView in different ViewController. Now I am showing an exactly same cell in both of them. So I want to create a reusable cell for convenience and easy maintenance.
I tried to create a xib and set the custom class to an UITableViewCell class, then I can register and load it in the UITableView. However, I cannot reuse this xib in the UICollectionView because CollectionView cannot load TableViewCell.
So my question is that is there any good way to make a reusable cell for both TableView and CollectionView?
UITableViewCell <- UIView
UICollectionViewCell <- UICollectionReusableView <- UIView
both are from UIView, so i think you can create a xib for UIView, and use that view in your UITableViewCell and UICollectionViewCell.
Eg: How to load a xib file in a UIView
and since both cell are going to have common code for some cases you can use category, to share the common code Eg: Sharing code between UITableViewCell and UICollectionViewCell
I have just created an app and have started hooking up #IBOutlet's to the storyboard. I am connecting some of them to labels in a UITableViewCell Prototype Cell with a Basic Style. When I connect it though I get this error in the Storyboard:
The detailText Outlet from the TableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
Can someone help me out? I have set it up the way I always do successfully but this time it has chucked me this error.
Create a table view cell subclass and set it as the class of the prototype. Add the outlets to that class and connect them. Now when you configure the cell you can access the outlets.
There are two types of table views cells provided to you through the storyboard, they are Dynamic Prototypes and Static Cells
1. Dynamic Prototypes
From the name, this type of cell is generated dynamically. They are controlled through your code, not the storyboard. With help of table view's delegate and data source, you can specify the number of cells, heights of cells, prototype of cells programmatically.
When you drag a cell to your table view, you are declaring a prototype of cells. You can then create any amount of cells base on this prototype and add them to the table view through cellForRow method, programmatically. The advantage of this is that you only need to define 1 prototype instead of creating each and every cell with all views added to them by yourself (See static cell).
So in this case, you cannot connect UI elements on cell prototype to your view controller. You will have only one view controller object initiated, but you may have many cell objects initiated and added to your table view. It doesn't make sense to connect cell prototype to view controller because you cannot control multiple cells with one view controller connection. And you will get an error if you do so.
To fix this problem, you need to connect your prototype label to a UITableViewCell object. A UITableViewCell is also a prototype of cells and you can initiate as many cell objects as you want, each of them is then connected to a view that is generated from your storyboard table cell prototype.
Finally, in your cellForRow method, create the custom cell from the UITableViewCell class, and do fun stuff with the label
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourCell
cell.label.text = "it works!"
return cell
}
2. Static Cells
On the other hand, static cells are indeed configured though storyboard. You have to drag UI elements to each and every cell to create them. You will be controlling cell numbers, heights, etc from the storyboard. In this case, you will see a table view that is exactly the same from your phone compared with what you created from the storyboard. Static cells are more often used for setting page, which the cells do not change a lot.
To control UI elements for a static cell, you will indeed need to connect them directly to your view controller, and set them up.
If you're using a table view to display Settings and other options (like the built-in Settings app does), then you can set your Table View Content to Static Cells under the Attributes Inspector. Also, to do this, you must embedded your Table View in a UITableViewController instance.
Or you don't have to use IBOutlet to refer to the object in the view. You can give the Label in the tableViewCell a Tag value, for example set the Tag to 123 (this can be done by the attributes inspector). Then you can access the label by
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "someID", for: indexPath)
let label = cell.viewWithTag(123) as! UILabel //refer the label by Tag
switch indexPath.row {
case 0:
label.text = "Hello World!"
default:
label.text = "Default"
}
return cell
}
With me I have a UIViewcontroller, and into it I have a tableview with a custom cell on it. I map my outlet of UILabel into UItableviewcell to the UIViewController then got the error.
As most people have pointed out that subclassing UITableViewCell solves this issue.
But the reason this not allowed because the prototype cell(UITableViewCell) is defined by Apple and you cannot add any of your own outlets to it.
Sometimes Xcode could not control over correctly cell outlet connection.
Somehow my current cell’s label/button has connected another cell
I just remove those and error goes away.
For collectionView :
solution:
From viewcontroller, kindly remove the IBoutlet of colllectionviewcell
. the issue mentions the invalid of your IBOutlet. so remove all subclass which has multi-outlet(invalids) and reconnect it.
The answer is already mentioned in another question for collectionviewcell
Click on simulator ,
Navigate to Window and enable Device Bezels
In my project, there is a UITableView which contains UICollectionViews in each row. Each UICollectionView contains a UIButton. I implemented those by using a tutorial here:
https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
The problem is, I need to add an action to button click. The usual IBAction click works, but I can't pass any data except an integer (tag) to it.
The question is, how do I pass some data to my button click action?
UIButton in UICollectionViewCell
UICollectionViewCell in UICollectionView
UICollectionView in UITableViewCell
UITableViewCell in UITableView
UITableView in UIViewController/UITableViewController
so your button outlet must be on collectionviewcell class and your button's action must be in uicollectionviewcell, and you can pass data from your uicollectionviewcell to UITableViewCell with delegate (uicollectionviewcell-delegate), and UITableViewCell to UIViewController/UITableViewController with another delegate.
I created UITableViewController with some cells. Inside some of those cells, there are another UITableViews with cells with the same layout. How to register cell in way to make it visible for table in another global UITableView:
UITableViewController:
UITableView main
UITableViewCell identifier1
UITableView2
UITableViewCell identifier3
UITableViewCell identifier2
UITableView3
UITableViewCell identifier3
All I need is to prevent create layout of cell in every subtable UITableView main. I would like to register cell in one cell, and use it in every another UITableView in this controller.