My tableOne contains custom UITableViewCell called CustomCellForTableOne (.h and .m). When user clicks on a cell in tableOne, I want them to segue to tableTwo. So far so easy.
I want the cell in tableOne to become the table header in tableTwo. Is there a simple straightforward way for doing this? Ideally, I want to simple create a CustomCellForTableOne from data that are passed through the segue and assign that cell as the header of my tableTwo.
I don't know how to assign header to a table programmatically (so I could try it)
Would it work? (since I haven't tried it yet anyway, I thought I might ask to save some time) Or must I take some different approach?
It should be possible as cell and header view for table both inherit from UIView. Implement the following in your tableTwo and return your table view cell instance from it. -
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section
Headers are hard to handle in table views. I would place an extra section in the second table only for that specific cell which has been passed through. This could be the first section of the second table and you could design it as it looks like a header.
Another way is to create a plain view and place it in the header of the second table (this is totally legal). Then you can safely add your specific cell (if it has a view) in that plain view via addSubView: .
I was able to achieve what you're looking to do with the following code in the first view controller:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// create the new table view
TestTableViewController2 *testView2 = [TestTableViewController2 new];
// get a reference to the cell that they clicked and create a totally new cell, a 'copy' of
// the original that we can pass to the next view
UITableViewCell *clickedCell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *clickedCellCopy = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
clickedCellCopy.textLabel.text = clickedCell.textLabel.text;
clickedCellCopy.detailTextLabel.text = clickedCell.detailTextLabel.text;
// set this new cell as the header cell on the new view
testView2.myHeaderCell = clickedCellCopy;
// push the new view onto the stack
[self.navigationController pushViewController:testView2 animated:YES];
}
Then add the property to your second table view controller:
#property (nonatomic, retain) UITableViewCell *myHeaderCell;
And then in the .m's viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// set our header cell as the table view's header
self.tableView.tableHeaderView = self.myHeaderCell;
}
That gave me the following result:
https://www.dropbox.com/s/t42lraue1kfolf3/cell%20demo.mov?dl=0
You could create your custom UITableViewCell in a xib file and load it for each view. You are going to have to create it in both view controllers. Depending how complex, you could also do this entirely in code.
Related
I'm trying to add another view controller inside a UITableView cell. The idea is that you tap the cell, and it expands to show more content--a messaging interface. It's important (I think) that this is controlled by a separate Messaging ViewController.
Expanding the cell and having views inside the cell expand with the proper constraints is actually very straightforward in Storyboards, so I tried to keep everything in storyboards by adding my new VC to the TableViewCell via a Container. That way I'd be able to add constraints on the container view, and pipe the content in from my Messaging VC.
Here's the error:
Illegal Configuration: Container Views cannot be placed in elements that are repeated at runtime.
Any way to get around this issue, or is there a way I can pipe the view from my viewcontroller into this tableviewcell and have it constrain to a configuration that I set in Storyboards? Thank you!
I had the same task and decided it this way:
Step 1. Create subclass MyCell: UITableViewCell.
Step 2. If you use Self-Sizing Cells, in InterfaceBuilder add UIView to MyCell, then add height constraint and constraints to all sides. This view needed for set height of cell.
If not, skip this step and use heightForRowAtIndexPath.
Step 3. In MyCell.h add outlet from view height constraint and controller property:
#interface MyCell: UITableViewCell
#property (weak, nonatomic) MessagingVC *controller;
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewHeight;
#end
Step 4. In cellForRowAtIndexPath add code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell" forIndexPath:indexPath];
// adjust this for your structure
cell.controller = [[UIStoryboard storyboardWithName:#"MessagingVC" bundle:nil] instantiateInitialViewController];
[self addChildViewController:cell.controller];
[cell.contentView addSubview:cell.controller.view];
[cell.controller didMoveToParentViewController:self];
// if you use Self-Sizing Cells
cell.viewHeight.constant = 200; // set your constant or calculate it
return cell;
}
Step 5. Add didEndDisplayingCell method:
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell isKindOfClass:[MessagingVC class]])
[((MyCell*)cell).controller removeFromParentViewController];
}
Make your UITableViewController content as Static.
You can just drag Container View into UITableVeiw in the storyboard. For example, you can drag it before prototype cell, and you will see your container's view controller before your prototype cells. By the way you can drag any UI element to table view. I'm not sure, how to deal with autolayout in the combination table view + container view, maybe you need to manually calculate / set constraints at the runtime. Will update my answer when I'll find the right solution about autolayout.
Putting container views in table view cells is way too heavy. Table view cells should be lightweight so the user can scroll through them quickly. It's not necessary to put the entire view controller in each cell. The cell should just represent some of the data for that row.
When the user touches the cell you just use a normal segue to the messaging view controller. Its presentation will be automatic. Then create and specify an animationController to handle the transition to make it appear as though the message composition view was contained within the table view cell.
I have a problem settings my view elements on a custom cell. The table cells appear in my tableView, but the properties do not set and thus only empty/blank cells appear.
The tableView is not a tableView controller, but only a tableView in a viewController.
I have the following files:
CustomCell.xib:
Here i use IB to build the custom cell by using a Table View Cell from object library with labels and images on. I set the Identifier as orderListCell. From this screen I ctrl+drag to create the outlets in customCell.h
CustomCell.h
Here I see all my IBOutlets as properties from above mentioned file
CustomCell.m
Here I leave as is
OrderListViewController.h
Here I import customCell.h and use protocols UITableViewDelegate, UITableViewDataSource
OrderListViewController.m
Here I set my tableView delegate and tableView dataSource to self. I also create an IBOutlet for my tableView from the Storyboard.
I use the following code to try and display my cells:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"orderListCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[CustomCell alloc] init];
}
cell.myLabel.text = #"aaaaaaaa"; //[[self.orders objectAtIndex:indexPath.row] objectForKey: #"tableNo"];
return cell;
}
I have simplified my code a bit to demonstrate that even setting the label to a simple string (#"aaaaaaaa") doesnt work. When I look at the objects in my debugger the cell does have all the properties from the IBOutlets and the cell does appear in my tableView, just the label.text = xxx does not seem to work.
I have looked at the following posts but either dont understand it properly or it does not work for me:
ios 7 customizing UITableViewCell's content view
Can't set properties in Custom UITableViewCell
Set label for a custom cell
Any help or advice would be greatly appreciated
cell = [[CustomCell alloc] init]; does not create your cell from the XIB, so none of the XIB content will be created.
Use registerNib:forCellReuseIdentifier: to register the XIB with the table view so that it will create your cell instances for you (you don't need to create instances yourself in cellForRowAtIndexPath).
If you don't want to do that, you can load your NIB explicitly with nibWithNibName:bundle: and instantiateWithOwner:options:, then get the cell instance from the list of returned views (which should only contain 1 item).
This is my first time using nib files, and I am using them because I was to toggle two views without bloating the storyboard for easy UI editing. I went with child view controller's so I could separate everything. One of the child controller's will contain table view delegate and data source. So I created a nib with a single view in IB. I dragged in a table view, but it is already doing something odd.
Here is what a table view dragged into a storyboard view controller looks like:
And here is what it looks like in the nib file:
It still has the default California data in it. Why doesn't it say "Table View Prototype"?
I tried to add a cell to it anyway but I couldn't. The first image is what normally happens, the cell is nested. In the nib, though, it won't go into the table view.
I want to make the table view with custom cells, but I don't know what is wrong or if I am missing something since this is my first time using nibs.
It still has the default California data in it. Why doesn't it say "Table View Prototype"?
Because prototypes are a feature of storyboards only - not nibs.
I tried to add a cell to it anyway but I couldn't. The first image is what normally happens, the cell is nested. In the nib, though, it won't go into the table view.
To use a nib-designed cell type with a table view that doesn't come from a storyboard, you must have a separate nib for just the cell (there must be nothing else in the nib). Register the nib with the table view (in code); from then on, when you dequeue a cell, the table view will load the cell from the nib. See this section of my book:
http://www.apeth.com/iOSBook/ch21.html#_custom_cells
It's normal that you cannot nest a Table View Cell inside a Table View in a xib file. One possible approach to custom cells with xib files is to create a subclass of UITableViewCell and set it as the file owner of your custom cell in the xib file (create a xib file for the cell). Then you will be able to create outlets between UI elements in your custom cell and your custom UITableViewCell subclass.
In the following example, I'll use MyCustomCell as the name of the xib file containing the cell, and also the name of the custom class derived from UITableViewCell.
To register the cell with your table view, put this in your UITableViewController subclass:
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *cellNib = [UINib nibWithNibName:#"MyCustomCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"MyCustomIdentifier"];
...
}
Finally, in your tableView:cellForRowAtIndexPath: method you can dequeue a custom cell using its identifier and set its visual properties using the outlets you created before:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyCustomIdentifier"];
// Configure cell using its outlets
return cell;
}
Edit: See my answer for a functioning app that somehow implemented what I was trying to do.
I've checked around for this and followed every available tutorial - this all seems pretty straightforward but my Storyboard & inspector simply will now allow me to do the following:
-- Add buttons to custom UITableViewButtons (using custom class 'Song Cell')
Every time I try to do so, it puts the button on a view which is above the table view. I've tried setting the cells to dynamic, static, basic, and every other toggle switch I could find.
I think its because I have a slightly awkward settup in terms of views, so I tried to set my TableView to a custom class as well. However, its not showing up in Storyboard's Class Inspector. Here is what I did, to set this Table View to a custom class, so no avail:
-- Create custom class inheriting from UITableViewController, called SongTableViewController
-- Set, in Storyboard, a table view controller's class to SongTableViewController
See this hierarchy:
// edit - Apparently I do not have 10 rep to post pics, so I'll just draw it myself:
▼ Voting View Controller - Current Songs
▼ View
▼ Table View // This is where I would like the custom class, SongTableViewController
> Song Cell
> Song Cell // These cells are where I would like to add the custom buttons
> Song Cell
> Constraints
> Label - 00:00
> Label - Voting will reset in:
Navigation Item - Current Songs
First Responder
Exit
When I select the Table View and go to the inspector to change its class, there is no option except for UITableView. Trying to hardcode this and hitting 'return' also does nothing.
Is my inability to add buttons to these cells due to the structure of
my views? Or something else?
Maybe you should create a xib with your custom cell. For exemple, the class "CustomeCell" with the xib "CustomeCell.xib".
You put some objects on your cell via xib file and in your class with your UITableView, do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
// configure cell
return cell;
}
Don't forget to link you datasource and delegate for your TableView in the xib file AND add the delegates in your UITableView class :)
Storyboards are usefull but sometimes, the good way is to use xib files :)
EDIT: You can read this tutorial, it's a very good example how to manage custom cell / tableview with xib files: http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Hope it help you :)
You probably don't want to change the class of the table view. It sounds like what you really want to do is change the class of one (or more) of the cell prototypes. In the storyboard select one of the cells and change its class to your song cell class.
Here is a description of what was going on, and an example of how to impliment this:
You have one UIViewController subclass, and add
the table view to it by dragging and dropping in the storyboard.
You then have a bit of extra work to do to fill the gap between a
table view controller and a plain view controller - declare that you
conform to the datasource and delegate protocols, create an outlet for
the table view, connect the outlet to the table view and connect the
table view's delegate and datasource outlets to your VC.
Implement the three datasource methods (number of sections, number of
rows and cellForRow...) and you're done.
Link to Prototype
I am wanting to create a custom UITableView cell. I would like to know how to do this. I understand how to actually create it and write code for it, but how can i create 1 style and then when i have more cells added, i want the same style. How can i do this? Is there a way to create 1 custom cell and have all the other cells that i want to add later follow this cells style?Thanks for the help!
In my projects I'm implementing method that creates custom style programmatically. Also it is possible to make custom cell via IB and when you need just take custom cell from it.
Don't forget that if you will write your code correctly then your cells will be reused and that method will be called only for number of cells that are visible in your table view.
may be this can help you http://iphone-bitcode.blogspot.com/2011/06/custom-tableview-cell.html
Write a separate .h/.m/.xib for the cell, and in the .xib set File's Owner to the class you want multiple copies of it in (your table view controller class, most likely). Attach it to an IBOutlet you created in the table view controller for new cells.
Then, each time you want a cell, try and dequeueReusableCellWithIdentifier: on your tableView, and if that doesn't work (you have no reusable ones), make a new cell using your custom class by simply loading the nib file. It will automatically create an instance of the cell and attach it to your IBOutlet, and then just retain the cell and set the outlet back to nil for the next time you need to create a cell. Essentially, I mean this (I have an IBOutlet UITableViewCell *cellOutlet):
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = #"CustomCell";
UITableView *cell = [self.tableView
dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"MyCustomTableViewCell"
owner:self options:nil];
cell = cellOutlet;
self.cellOutlet = nil; // autoreleases
cell.reuseIdentifier = reuseIdentifier;
}
// configure the cell here
return cell;
}