how can i programmatically put a uicollectionview within an expanding uitableviewcell - ios

i am trying to put a UICollectionview inside of an expanding uitableviewcell, but i keep getting errors. the collectionview is also suppose to take pictures and store them within the collectionview. i used this code:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath (NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"myCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[cell addTarget:self action:#selector(uploadPicture:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)uploadPicture:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
the errors i am getting are "no visible #interface for UICollectionViewCell declares the selelctor addTarget:action:forControlEvents" in line 4. and line 5 "void method should not return value" and line 11 "no visible #interface for ExpandingCell declares the selector presentViewController:animated:completion"
i am desparate for help at this point. so thank you in advance if you do help.

Quite a few issues here.
Line 4 - you are in the didSelectItemAtIndexPath method, so you know the user has already touched the cell. You should not need to wire up a target action. Remove this line and call uploadPicture: directly.
Line 5 - didSelectItemAtIndexPath is a void method, so you should not try to return cell. Remove this line.
Line 11 - the error indicates your uploadPicture method is not within a view controller. Therefore it cannot present a view controller. You need to move this method to your view controller or refactor your code so that you present from within a view controller.

Looks like you want to achieve the following:
User has a tableview, and when user taps on a row in table view, app presents an image picker
User selects a number of images from library
All selected images are then shown in a collection view which is embedded within the expanded cell of the tableview
If that assumption is correct, you have to:
implement didSelectRowAtIndexPath for the tableview instead of collectionview
Within this method, call the uploadPicture method
implement didFinishPickingMediaWithInfo of imagePickerController to get selected images
call cellForRowAtIndexPath of the tableview controller to create cell with collectionview having the selected images.
Hope this helps.

Related

Changing custom tableview cell button background image using NSNotification from non-tableview host class

1) I have a simple table view hosted by view controller and cell hosted by custom UITableViewCell class. A button on my cell drops down a menu (a simple table view controller loading from the non-host 'out of the picture' class using FPPopoverMenu).
2) The problem is I want to update my button background image on dropdown menu row selection which involves my 'out of the picture dropdown menu tableview class' and my 'custom table view cell class' totally deserting the host of my custom UITableViewCell.
3) I tried using NSNotification like, I successfully did for a simple scenario involving only host-class and dropdown menu class but now its the custom tableview cell (which is a repeating entity) and dropdown class I want to communicate.. Please help. I set up NSNotification but background image stays the same, means notification doesn't reach/doesn't reach in time.
4) Apparently I need 10 reputation to post image (:-[) so here's the link:
As shown by the image, I have fired notification on dropdown's didSelectRow, when Hide is pressed, background should change otherwise, if show is pressed it should be green as shown..as i did before but this doesn't do anything for me. Any help will be greatly appreciated. thank you in advance!
To achieve this you can use blocks.
You need to add
#property (nonatomic, copy) void (^didSelectAction)(NSIndexPath *indexPath);
to view controller which is shown in popover.
than in tableView: didSelectRowAtIndexPath: call this block
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.didSelectAction)
self.didSelectAction(indexPath);
}
So when you create a popover you should provide additional handler.
Something like this
Add new action to your button
- (UITableViewCell *) tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
[[cell button] addTarget:self action:#selector(showPopoverFromButton:) forControlEvents:UIControlEventTouchUpInside];
}
- (void) showPopoverFromButton:(UIButton *)sender {
//Your table view which is shown in popover
UITableViewController *controller = [[UITableViewController alloc] init];
[controller setDidSelectAction:^{
[sender setBackgroundColor:[UIColor redColor]];
}];
FPPopoverMenu *popover = [[FPPopoverController alloc] initWithViewController:controller];
[popover show];
}

iOS: Storyboard CollectionView segue not being triggered

I have a UICollectionView controller embedded inside a navigation controller. The collectionView lists projects and each cell is supposed to segue to a ProjectDetail screen.
I simply cannot get the segue to trigger. If I simply drop a button on the nav bar and hook up a segue to the detail, it works. But triggering from my CollectionView cell doesn't.
Here is what the storyboard looks like: http://cl.ly/RfcM I do have a segue hooked up from the CollectionViewCell to the ProjectDetailViewController
Here's the relevant code inside my ProjectDetailViewController:
#interface ProjectCollectionViewController () {
NSArray *feedPhotos;
Projects *projects;
}
#end
#implementation ProjectCollectionViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[FeedViewCell class] forCellWithReuseIdentifier:#"cell"];
[self loadData];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"selected %d", indexPath.row);
Project *project = [projects getProject:indexPath.row];
NSLog(#"project = %#", project);
}
- (void)loadData {
[self.projectLoader loadFeed:self.username
onSuccess:^(Projects *loadedProjects) {
NSLog(#"view did load on success : projects %#", loadedProjects);
projects = loadedProjects;
[self.collectionView reloadData];
}
onFailure:^(NSError *error) {
[self handleConnectionError:error];
}];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return projects.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
FeedViewCell *cell = (FeedViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
UIImageView *cellImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
Project *project = [projects getProject:indexPath.row];
NSString *imageUrl = [project coverPhotoUrl:200 forHeight:200];
NSLog(#"imageurl =>%#", imageUrl);
if (imageUrl) {
[cellImageView setImageWithURL:[NSURL URLWithString:imageUrl]];
}
[cell addSubview:cellImageView];
cell.imageView = cellImageView;
return cell;
}
I'm guessing the problem is somewhere in how I'm hooking up the Cells to the CollectionView.
Any help would be greatly appreciated!
You cannot create segues directly from cells in a storyboard because the collectionview is populated dynamically through the data source. You should use the collectionView:didSelectItemAtIndexPath: and perform the segue programatically using performSegueWithIdentifier:sender:. Something like this:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"MySegueIdentifier" sender:self];
}
where MySegueIdentifier is the identifier of the segue defined in storyboard.
TLDR: FOR A STORYBOARD, do not call registerClass:forCellWithReuseIdentifier:. It overrides what the storyboard sets up for the cell (including how segues are handled):
How to set a UILabel in UICollectionViewCell
Brief setup
Used a storyboard
Created a new collection view controller using the Xcode template,
setting it as a subclass of UICollectionViewController.
Initially used the default UICollectionViewCell, adding a UILabel
programmatically.
The generated UICollectionViewController code registered the cell in viewDidLoad:
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
First Issue:
The prepareForSegue:sender: event was not firing, which brought me to this answer .
I implemented the UICollectionViewDelegate and collectionView:didSelectItemAtIndexPath: event, then called the segue programmatically.
This fixed my first issue.
Second Issue: I switched to a custom cell containing one label. After hooking everything up, the cell label was not displaying.
After some digging, I found a solution contained in the link at the top of my answer.
Third Issue and Solution: I removed the registerClass:forCellWithReuseIdentifier: line. When I ran my app, the label appeared correctly, but when I tapped a cell, it called the prepareForSegue:sender event twice. By removing the registerClass:forCellWithReuseIdentifier line, the cell was processing cell touches directly, without the need of the delegate method. This is how I expected the storyboard to work. I deleted the collectionView:didSelectItemAtIndexPath: event, which resolved the double-firing of prepareForSegue:sender:. If you are using a storyboard, do not register the cell class. It overwrites what storyboard sets up.
Have you made your CollectionView Cell's connection in Triggered Segues on selection?
You can also trigger a segue programatically using
[self performSegueWithIdentifier:#"segueIdentifier" sender:nil]
in
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Equivalent Swift code for similar question.
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier(#"TargetSegway", sender: self)
}
Make sure, in case if your cell has other overlapping views, "User Interaction Enabled" is unchecked (you can find this option, under attribute inspector View/Interaction). Otherwise, your Tap Gesture is consumed by the overlapping view, didSelectItemAtIndexPath may not be called.

Is it possible to switch view to tableview from same view?

I have a viewcontroller.xib which contains View, buttons,toolbarbutton, text box and a tableview. When I load the initial screen comes without table view which is fine. Now when I click on a toolbarbutton say, viewtable, I want the view to move to tableview. I have filled my tableview data with some default objects like this:
- (void)viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"object1",#"object2",#"object3",#"object4", nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"My Cell"];
if(cell==nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"My Cell"];
}
cell.textLabel.text=[tableData objectAtIndex:indexPath.row];
return cell;
}
So when I click on toolbar view button it should show tableview with toolbar button which also has a back button so when I click on that it should hide the table view and show the initial view. Is it possible to do all this in single xib? I can achieve the result if I create another xib and simply transfer control over to that xib but I wanted to know if its possible do this without creating a second xib file. And also for navigation I can use navigation controller but I want to check and see if its possible to use toolbar to transfer the control. Thanks.
First check if your table view is inside your view, if not put it inside and set delegate of datasource to file owner, then in your view table method write this code
-(void)viewTable
{
self.tableView.hidden = NO;
self.viewToolbar.hidden=YES;
}
On your back button code in toolbar write
-(void)goback
{
self.tableView.hidden = YES;
self.viewToolbar.hidden=NO;
}
If you don't need animation then you can do the following
Get a handle of tableView in your interface like this:
#property(nonatomic,assign)IBOutlet UITableView *tableView;
Hide your table view in initially ( like in viewDidLoad method )
-(void)viewDidLoad
{
[super viewDidLoad];
self.tableView.hidden = YES;
}
Then in the method called by your toolbar's button do the following
-(void)on_click_toolbar_button
{
self.tableView.hidden = !self.tableView.hidden;
//This will keep toggling the table view from hidden to shown & vice-versa.
}
you could use the hidden property to achieve that. Put these in the appropriate ibaction methods.
_tableView.hidden = Yes;
_tableView.hidden = No;
I'd highly recommend to do this in two separate XIBs. The first should contain a UIViewController (your initial view) and the second a UITableViewController (your table view) class. Both should be handled by a UINavigationController - don't fight the API and try your own hacks if it's not necessary. The mentioned controller classes give you everything you need out of the box.
Well this is not recommended but you can do this by removing and adding tableview..

Use a button on a UICollectionViewCell to display data from an array

I have an array of NSStrings, one UILabel & a UICollectionView.
My Question:
I want the array's count to determine how many UICollectionViewCell's there are.
Each UICollectionViewCell contains a button. Upon click, I want this button to cause the data in the array that corresponds to the UICollectionViewCell's number to be displayed in the label.
For example, if the user clicks on the 13th UICollectionViewCell's button, then the 13th NSString in the array would become the UILabel's text.
What I have done:
I have made my own subclass of UICollectionViewCell for the nib file that I use for all of the UICollectionViewCells, & connected the button to the .h file as a IBAction. I have also imported the MainViewController.h, which is the one that contains the array property that stores the NSStrings.
When I edit the code in the UICollectionViewCell's action, I cannot access the array property. The button does work - I placed an NSLog in the IBAction's method, which does work.
I have searched through tens of other answers on SO, but none answer my specific question. I can update this with samples of my code if requested.
I have made my own subclass of UICollectionViewCell for the nib file
that I use for all of the UICollectionViewCells, and connected the
button to the .h file as a IBAction.
If you connect the IBAction to the subclass of collectionViewCell you would need to create a delegate to make the touch event available in the viewController where you are displaying the data.
One easy tweak is to add the button the collectionViewCell, connect it's IBOutlet to the cell. But not IBAction. In the cellForRowAtIndexPath: add an eventHandler for button in that viewController containing collectionView.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//Dequeue your cell
[cell.button addTarget:self
action:#selector(collectionViewCellButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)collectionViewCellButtonPressed:(UIButton *)button{
//Acccess the cell
UICollectionViewCell *cell = button.superView.superView;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSString *title = self.strings[indexPath.row];
self.someLabel.text = title;
}
Please try like this..
In YourCollectionViewCell.h
Create an IBOutlet not IBAction called button for the UIButton that you added to the xib. Remember you should connect the outlet to the cell object not to the file owner in the xib.
MainViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
cell.button.tag = indexPath.row;
[cell.button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)buttonPressed:(UIButton*)sender
{
NSLog(#"%d : %#",sender.tag,[array objectAtIndex:sender.tag]);
self.textLabel.text = [array objectAtIndex:sender.tag];
}
Edit- Handle multiple sections
-(void)buttonPressed:(UIButton*)sender
{
NSIndexPath *indexPath = [self.collectionView indexPathForCell: (UICollectionViewCell *)sender.superview.superview];
NSLog(#"Section : %d Row: %d",indexPath.section,indexPath.row);
if (0 == indexPath.section) {
self.textLabel.text = [firstArray objectAtIndex:indexPath.row];
}
else if(1 == indexPath.section)
{
self.textLabel.text = [secondArray objectAtIndex:indexPath.row];
}
}
When I edit the code in the UICollectionViewCell's action, I cannot access the array property.
That's because you connected the button action to the "wrong" object. It needs to be connected to the MainViewController (or whoever it is that does have access to the array property).
You are going to have several tasks to perform:
Receive the button action message.
Access the array (the model for the data).
Throw a switch saying which cell should now have its label showing.
Tell the collection view to reloadData, thus refreshing the cells.
All those tasks should most conveniently belong to one object. I am presuming that this is MainViewController (and thus I am presuming that MainViewController is the delegate/datasource of the collection view).

UIView & UITableViewCell not being deallocated when UIView becomes the delegate of a UITableViewCell

I have a view controller (EmbeddedMenuView) that uses a custom view (HorizontalMenuView). The Embedded menu view uses multible HorizontalMenuViews. The HorizontalMenuView contains a UITableView. Each cell in the table view uses quite a bit of memory (high quality images.).
Now, I need to execute a task every time a section of the table view cells in the HorizontalMenuView is touched. I did this by creating a protocol in the table view cell and assigning the HorizontalMenuView its delegate. Then I created a protocol in the HorizontalMenuView and assigned the EmbeddedMenuView its delegate. So I pass the touch event up to the EmbeddedMenuView.
The problem is, when I assign the cell's delegate, the HorizontalMenuView does not get deallocated. Since this view refreshes itself every time the view appears, the memory footprint gets out of control fast.
If I comment out the part where the cell is assigned a delegate, everything works fine.
My question is: How can I properly release a UITableViewCell's delegate?
This is the code snippet from the HorizontalMenuView:
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Custom Logic
HorizontalMenuItemTableViewCell *cell = (HorizontalMenuItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[NSClassFromString([[AMPUIManager sharedManager] classNameForName:cellIdentifier]) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.shouldAlwaysTransform = shouldAlwaysTransform;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.colorsDict = colorsDict;
if ([cell isKindOfClass:[ATCustomTableViewCell class]]) {
((ATCustomTableViewCell *)cell).delegate = self; //Commenting this out solves my problem.
}
}
//More Custom Logic
return cell;
}
PS I am using manual reference counting. ARC is not an option for this project.
It sounds like you may have a circular reference. You almost always want to use 'assign' convention with delegates.
See: Why are Objective-C delegates usually given the property assign instead of retain?

Resources