Swift Collection View to Table Cell - swift

I have a collection view that's working fine linked to a detailViewController. However I also need to link it to a CustomCell class that's used in a table. I do not know how to set the datasource or delegate and I normally do this via the GUI in storyboards.
Should I have the collectionView in the customCell class?

Related

How/where do I create a table view cell subclass and set it as the class of the prototype

I have a second viewcontoller and was linked to this
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.
But I do not understand where I put the subclass and how to make one.
Outlets cannot be connected to repeating content iOS The link I was sent to.
First, create the file
The same way that you create a UIViewController or UITableViewController: In your project, go to New File. Select Cocoa Touch Class under iOS. The subclass is UITableViewCell and the name is whatever name you're giving it.
Within that class, you drag your buttons, labels, etc to create your outlets.
Assign the class to the cell
Remember to assign the class name to the cell in the identity inspector:
Put it in code
You will need a slight change in your cellForRowAtIndexPath method:
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyCustomCell
where "myCell" is the identifier you've given your cell in the storyboard, and obviously MyCustomCell is the new class you've created to which you are casting your cell.

Sending custom cell through segue

I currently have two UITableViewControllers, one with the prototype of the custom cell, and the other one not. Since both of them are connected through a push segue from the first to the second. I pass the selected custom cell to the second tableView.
Everything works fine, except that when I come back to the first view, the selected (and passed) cell is hidden, but when I scroll it enough to hide the blank space and scrolling back it shows.
How can I fix this?
The Big Nerd Ranch Guide says basically to stay away from Storyboard for just this kind of reason.
Amongst other things about Storyboards, it says:
"Overall storyboards make easy code easier and difficult code more difficult".
Not sure you should be passing a cell through the push segue.
When not using storyboard, you would typically create a new file of type UITableViewCell called e..g MyCustomerTableviewCell.
Then in your UITableView viewDidLoad method, create a new Nib and add it to the tableView,
UINib *nib = [UINib nibWithNibName:#"MyCustomTableViewCell" bundle:nil];
// register this nib that contains the cell
[[self tableView] registerNib:nib forCellReuseIdentifier:#"MyCustomTableViewTableViewCell"];
Then create a new empty XIB file (file->new->User Interface->empty) and call it MyCustomTableViewCell.xib.
Drop a UITableViewCell into the interface builder space, change it from UITableViewCell type to MyCustomTableViewCell, make sure it's File's Owner is MyCustomTableViewCell.
Then in your UITableView file, do this...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomTableViewTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:#"MyCustomTableViewCell"];
...
return cell;
}
You can probably do this as part of a storyboard project anyway (though I haven't tried missing XIB and Storyboard in one project).
If I understand correctly what you are doing, you are passing a reference of the tapped cell to the second view controller, and then you are showing it inside the table view of the second controller passing the same reference in the cellForRowAtIndexPath method.
It doesn't seems a good choice :-)
First: because that cell was allocated and queued for use in the first view controller. At any time, the cell could receive a prepareForReuse message by the first view controller.
Second: when you show the cell in the second table view, the cell is removed from the first table view.
If you don't want to create another prototype cell in the second view controller, you can, for example, allocate a new cell in the first one, copy all properties from the tapped one to the new, and then pass the new to the second viewcontroller.
This is the first solution I've thought about, but sure there are other better solutions :-)

how to use a UITableView without a UITableViewController

I want to use a UITableView as a single selection in my own ViewController, now I can display it but fail to fill the cell or get wrong on how to fill the cell's textlabel. Any suggestion? ...
Implement the protocols for UITableViewDataSource, UITableViewDelegate in .h or .m then connect the tableview with the first responder -datasource and -delegate in interface builder and support the protocol required classes in your .m.
For datasource it is: cellForRowAtIndexPath and numbersOfRowsInSection.
Set your cell.textlabel.text in your cellForRowAtIndexPath method.
You have to register an object as its datasource if you want to fill it.
If you also want to receive events, you also have to implement a delegate
You can embed a TableViewController in your ViewController (property) and add the TableViewControllers view to your ViewControllers view (addSubView). Creating cells goes into the TableViewController and works the same way as in the usual case.

iOS5 Storyboard: Reuse Custom UITableViewCell in multiple UITableViewControllers

Using a dynamic, custom cell prototype that I design in, say, UITableViewController A (in Interface Builder/Storyboard), works really well with dequeuing the cell (through its identifier, cellA) and such in cellForRow... I use a custom class (UITableViewCell subclass, let's name it MyCustomCell) to link up the labels and image thumb and it works all pretty well and straight forward in my UITableViewController A.
Now I create a UITableViewController B (in Storyboard), which happens to have the exact same design/functionality for its custom cells (dynamic cells). So I switch the class of these cells to the MyCustomCell and give it a new identifier, cellB.
In UITableViewController B, I dequeue the cell in cellForRow... and use the new identifier cellB. Note: Using cellA here leads to a crash, more or less obviously.
But when the table view shows up, while running the app, the UITableViewController A works just fine, and the almost identical UITableViewController B does not work (empty cells).
In Storyboard, it looks sort of off a bit, because the custom cell is designable within the UITableViewController A but in UITableViewController B, it's just a simple, plain cell. Despite the class associated to MyCustomCell.
How would one avoid copy&pasting these cells to the other controller (and therefore heavily going back and forth between copies when making design changes) – and rather just properly re-use it?
What you are doing is correct. I don't know why its not working, it may be some problem with reloading the tableview; check with your datasource and the datasource method.
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section
rather what you can do is: drag a
Viewcontroller
and then drag a
tableview
and make it dynamic and do the thing that you did earlier; it worked fine for me when I did so myself.
Saying the Cell is from a your custom class doesn't mean it's "designed" the same. What you really try to achieve here (and what I'm looking for) is some king of "Contained" Cell, but this is only doable with a ContainerViewController in iOS 6 I think. The other option is to use a XIB for that one Cell — that should work just as good, but then you lose the benefits of having an overview in the Storyboard.
I had a similar problem, I had created a custom cell with a uitextfield inside it. It was working well in the first tableview but not in the second. I figured out that the textfield's delegate was not connected to the custom cell. I connected the two in storyboard by making a connection from textfields delegate outlet back to the textfield.

How to add cells to UITableView that has no controller?

I want to add some data from a NSMutableArray to my UITableView. I have made an IBoutlet which is called lessonsTable. All the tutorials i can find assumes that the UITableView has its own controller, so they change a method about adding cells. But i don't have a separate controller class for my UITableView, it is just a view floating in another controller.
I've tried
[lessonsTable insertRowsAtIndexPaths:appDelegate.lessonsArray withRowAnimation:UITableViewRowAnimationTop];
But it didn't solved the problem.
lessonsArray has the data that i want to insert to my UITableView
You don't need a dedicated view controller, but you do need to implement the UITableViewDelegate and UITableViewDataSource protocols. You can't just send an array to a table and expect it to figure out the details for you.