I have tableView in first controller and i am making it with tableviewcells defined in xib. Now when i click on one tableviewcell i need to open a present view controller and pass on the View of tableviewcell to second view controller.
I am using a init method in second view controller and passed the cell.contentView of 1st VC to 2nd VC.
It is working fine tableviewcell content view is getting shown on the 2ND VC first table section but when i press close to the 2nd VC .The TableViewCell content view disappears from first VC.
I am not able to so. I have seen on net they are saying register XIB again on 2nd VC , or use archive and unarchive but these are time consuming operations and i just want to pass a tableviewcell content view to 2nd vc and when i close 1st VC i want it back there.
Any help would be appreciated.
View is the cell.contentView of the pressed tableview cell
self.superViewCell = View;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HVAPParentTableViewCell * cell = [[HVAPParentTableViewCell alloc] init];
[cell.contentView addSubview:self.superViewCell];
return cell;
}
The cell is probably missing in the 1st view controller because you removed its contentView and passed over to the 2nd view controller. This is clearly an anti-pattern you probably don't want to use. You may want to pass over to the 2nd view controller cell's "state" rather than its contentView though, and initialise a new cell with the same cell data.
Having little knowledge about what you're actually trying to achieve, my general advice would be to register the 2nd view controller's table view with the same xibs and cell identifiers as in the 1st view controller. Now you can pass over only the selected cell's identifier (so that you know which one to use when dequeueing in cellForRowAtIndexPath for the passed-over cell) and cell's data. Then you can initialise the same cell in 2nd view controller with cell's data from 1st view controller.
Related
In my app there is a table view with custom cell.in that cell there are (image view, views and labels).data loads succuessfully to the table.
Then what I wanted to display another view(detail view) when user tap on the cell.
I did it by (ctrl+drag) cell and connect it with the detail(second) view.
and selected the segue kind as Show.so then,when I click on a cell first time it correcly and and then I have to tap twice to load the detail view. I tried with changing kind to show Detail, present Modally, etc.but the no successful result. Then I tried with using navigation controller within these views.but didn't get any successful result.no idea what to do.I added some code and if you guys want more code I can provide.hope your help.thanx
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FlightRoundtripDetails *setDetails = [RoundItineraryArray objectAtIndex:indexPath.row];
resultCell.firstdepatairiportCodeLabel.text = setDetails.FirstDepartureAirport;
resultCell.firstarriairportCodeLabel.text = setDetails.FirstArrivalAirport;
resultCell.firststopoverLabel.text = setDetails.Outbound;
}
data display correcly.only the issue is when tap a cell.hope your help.thanx.
I think u have set the tableview selection as Multiple. So goto Attributes Inspector and change the selection to Single.
In my app, I am using tableview and cell in this which is tableview too. All things work good but when I want to handler selected cell on tableview which is a cell but i don't control
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
This function call at tableview parent only. But in my case, i want to call this function in both of tableview (parent tableview and tableview in cell).
Thanks!
Did you imported UITableViewDelegate? and add it to you table view as self.tableView.delegate = self,Better to check once!
Inside the table view of the cell, please set the delegate and datasource as the cell views reference.
As I think, your parent tableView get the select action and block the child table view from receive this action. I think there're two or more approaches to resolve this:
1. Change your view hierachy (contruct) of your view (don't make detail table view as a cell of the parent table). Make the detail table view as top view, so it could received select action, then maybe, pass this action to (parent) tableview.
2. Add UIGestureRecognizer to both child tableView and parent tableView, then check to perform tap action to both of them
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
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.
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