Adding buttons to custom Table View Cell under custom TableView Class - uitableview

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

Related

iOS: create table "header" programmatically from existing cell

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.

xcode document outline has extra content view

I am learning objective c and I'm following this tutorial here:
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
Whenever I create a table cell view, It creates an extra content view that makes it impossible to link my labels, images to the cell. How Can I delete the extra content view and link my objects?
Here is a screenshot showing what I mean...
Getting an Error with this code... use of undeclared identifier on cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MingleViewCell *cell = (MingleViewCell *)[[tableView dequeueReusableCellWithIdentifier:#"MingleViewCell"]];
LocalPerson *local = [self.people objectAtIndex:indexPath.row];
cell.nameLabel.text = local.name;
cell.aboutMeLabel.text = local.game;
return cell;
}
Relax. From the docs:
The content view of a UITableViewCell object is the default superview for content displayed by the cell. If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.
This is how it's done, the content view should be there and you should be able to connect IBOutlets/IBActions normally by ctrl-dragging from your elements added to the cell in the storyboard into the custom cell class code.
you should not delete the content view which you are calling is extra. If you see UITableViewCell has a property contentView by default in which you add all the other content like UIButton, UILabel, etc. It's this contentView which is the main container.
So whenever you inherit any class from UITableViewCell, that class also has this contentView. So instead of deleting it you can use it as a container and add other elements.

Child View Controller Nib with TableView

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;
}

UIButton in custom UITableViewCell stop working after rotation

Problem:
I used custom UITalbeViewCell that contains two buttons, they work fine in the portrait orientation. After the rotation, they all stop responding to the button touch up inside function. Some people having problems that their buttons couldn't drew correctly after rotation. Mine looked fine since the buttons are showing in the right places after rotation, but they do not respond to the button press anymore.
For this specific view in my app, I used a UIPageController to implement multi pages in a view, and for the view (name it EmbeddedView for now) embedded in the page's scroll controller, there is a UITableView that contains custom UITableViewCell. Custom table view cell only has a nib, the file's owner is EmbeddedView.
in EmbeddedView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*===== This is the most memory efficient way of creating table view cells =====*/
static NSString *CellIdentifier = #"CellIdentifier";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[self customTableCellNib] instantiateWithOwner:self options:nil];
cell = [self customTableCell];
[self setCustomTableCell:nil];
}
}
What I tried:
I created another nib file for the custom table view cell and used it in -cellForRowAtIndexPath(), I checked the orientation and dynamically create the cell by using different nib, no luck.
I added [tableview reloadData] in -didRotateFromInterfaceOrientation(), didn't do anything.
Would someone point me to the right direction please? Any help is appreciated.
This is the table view Autosizing in IB:
It looks right but the buttons are not working
Update: I tried to specify different Autosizing masks in IB for the table view, and the results are showing below:
<1>
<2>
<3>
<4>
Have you checked how the superview is being resized?
Check if the superview has 'clip to bounds' checked. If it is not check it. That will make the view clip its contents so you see if it is resizing ok.
I'd say the superview is not sizing correctly and because of that the touch events are not well delivered also.
EDIT - So this was the tip that could let the OP reach the solution:
What I normally do in cases like of unexpected behavior in resizing and such is to change every view in the hierarchy to a different, well recognizable, color. Right now you have view A and view B with the same background color (or clear) and you don't see if view B is resizing well. Good luck.

Added table view cells follow 1 custom style?

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;
}

Resources