1
votes

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?

3

3 Answers

1
votes

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 :)

0
votes

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.

0
votes

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