Issue with Reordable CollectiveView - Objective C - ios

I am using the LXReorderableCollectionViewFlowLayout to work with my needs here for being able to rearrange my CollectionView. I've been implementing the basics to get that demo working with my code and I keep hitting a road block with:
PlayingCard *playingCard = self.deck[indexPath.item];
PlayingCardCell *playingCardCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"PlayingCardCell" forIndexPath:indexPath];
playingCardCell.playingCard = playingCard;
return playingCardCell;
PlayingCardCell.playingCard = playingCard keeps giving me [UICollectionViewCell setPlayingCard:]: unrecognized selector sent to instance 0x155620c30' Now I understand that this is not seeing the instance but I've implemented all of my header files associated and tried to even make that instance public.
- (void)setPlayingCard:(PlayingCard *)playingCard {
_playingCard = playingCard;
self.playingCardImageView.image = [UIImage imageNamed:_playingCard.imageName];
}
My setup is based off a UITabBarController that switches ViewControllers, within this specific view I have a custom ViewController class that created a custom UICollectionController.
Maybe its because it is a ViewController rather than a CollectionView?
Suggestions, thoughts?
https://github.com/lxcid/LXReorderableCollectionViewFlowLayout

It looks like the [collectionView dequeueReusableCellWithReuseIdentifier:#"PlayingCardCell" forIndexPath:indexPath] is actually returning a UICollectionViewCell instead of your custom class.
Do you create your cell appearance in a storyboard? If so, can you check that in the storyboard, your cell has the custom class set to PlayingCardCell (your custom collection view cell class) and not to UICollectionViewCell?

Related

Getting data out of custom UITableViewCell and back into UITableViewController

I have a UITableView comprised of custom UITableViewCells. In each cell, there is a UILabel and a UISlider. Does anyone know how to, upon a change in value of one of the sliders, send the new value of the slider from the custom UITableViewCell (in a separate file) to the UITableViewController, so that I can then update the array from which the table was populated?
The closest I've got so far is a failed hack: firing a setSelected event when a slider value is changed. Whilst this highlights the changed custom cell, the event is not picked up by didSelectRowAtIndexPath in the UITableViewController.
Whilst code is always appreciated, a conceptual/method solution is what I am looking for.
Thank you in advance,
Jamie
What you need is called Delegate Pattern.
Quoting from there to explain what does it mean:
Delegation is a simple and powerful pattern in which one object in a
program acts on behalf of, or in coordination with, another object.
The delegating object keeps a reference to the other object—the
delegate—and at the appropriate time sends a message to it. The
message informs the delegate of an event that the delegating object is
about to handle or has just handled. The delegate may respond to the
message by updating the appearance or state of itself or other objects
in the application, and in some cases it can return a value that
affects how an impending event is handled. The main value of
delegation is that it allows you to easily customize the behavior of
several objects in one central object.
These diagrams will help you understand what goes on:
Architecture:
Operation:
Now as to how to implement it, this is what you have to do.
For Objective-C:
First of all, create delegate methods of your UITableViewCell. Lets name it ContactTableViewCell.
In your ContactTableViewCell.h file, do this:
#protocol ContactCellDelegate <NSObject>
#required
-(void) didMoveSliderWithValue:(float) value;
#end
#interface ContactTableViewCell : UITableViewCell
#property (weak, nonatomic) id<ContactCellDelegate> delegate;
Now conform your TableViewController to this delegate. Let's name your VC MyTableViewController.
In MyTableViewController.h, Do this:
#interface MyTableViewController : UIViewController <ContactCellDelegate> //Use UITableViewController if you are using that instead of UIViewController.
In your cellForRowAtIndexPath, before returning cell, add this line:
cell.delegate = self;
Add implementation for the delegate method INSIDE your MyTableViewController.m.
-(void) didMoveSliderWithValue: (float) value
{
NSLog(#"Value is : %f",value);
//Do whatever you need to do with the value after receiving it in your VC
}
Now let's get back to your ContactTableViewCell.m. In that file you must have added some IBAction to capture the value change event in slider. Let's say it is the following:
- (IBAction)sliderValueChanged:(UISlider *)sender {
self.myTextLabel.text = [#((int)sender.value) stringValue]; //Do whatever you need to do in cell.
//Now call delegate method which will send value to your view controller:
[delegate didMoveSliderWithValue:sender.value];
}
When you call delegate method, it will run the implementation that we wrote earlier in the MyTableViewController. Do whatever you need in that method.
What happens here is that your Cell sends the message to your desired VC (Which is delegate of the Cell), that "Hey, Call the delegate method that we wrote earlier in your body. I am sending you parameters right away". Your VC takes the parameters and does whatever you wanted it to do with that info and at that time.
For Swift:
First of all, your TableViewCell.swift file, create a protocol like this:
#class_protocol protocol ContactCellDelegate {
func didMoveSliderWithValue(value: Float)
}
Now in your Cell class, create a delegate property like:
var cellDelegate: ContactCellDelegate?
In your Slider IBAction, call the delegate method like this:
self.cellDelegate?.didMoveSliderWithValue(slider.value)
In your VC do these changes:
Make it conform to the delegate:
class MyTableViewController: UIViewController, ContactCellDelegate
Add this line before returning cell in cellForRowAtIndexPath
cell.cellDelegate = self //Dont forget to make it conform to the delegate method
Add the implementation of required delegate method:
func didMoveSliderWithValue(value:float) {
//do what you want
}
I have kept the Swift part precise and summarized because It should be very easy to change the detailed Obj-C explanation to Swift implementation. However If you are confused about any of the pointers above, leave a comment.
Also see: StackOverflow answer on using Delegate pattern to pass data back

IBOutlet UILabel not updating after delegate call

Screencast: https://www.youtube.com/watch?v=ZwehDwITEyI
This is really bizarre. The problem is to do with a label outlet sitting in a custom-designed table cell. That cell is of my CustomCell class. (actually called RA_FormCell if you watch the screencast).
CustomCell.h
#property (weak, nonatomic) IBOutlet UILabel *dayOutlet;
-(void)controller:(id<CustomCellDelegate>)controller didUpdateDay:(NSString *)theDay;
CustomCell.m
// Method is called by a view controller
// (which is itself a delegate of the CustomCell class,
// hence the identifier you see below)
-(void)controller:(id<CustomCellDelegate>)controller didUpdateDay:(NSString *)theDay;
{
NSLog(#"Method called") // confirms to me that method is called
self.dayOutlet.text = #"Goodmorning";
NSLog(#"%#", self.dayOutlet.text); // displays (null)
}
That final log does actually appear, so the method is definitely being called. I have discounted the following:
self.dayOutlet.text is not written to elsewhere by any other method in the project
dayOutlet is connected to the label in the storyboard (and the label is not connected to anything else)
The label is not hidden underneath some accidental static label on the storyboard
The cell attributes on the storyboard include its class as CustomCell
No warnings or alerts in Xcode (I have been careful to avoid any circular imports)
The problem was that the controller:didUpdateDay: message was not sent to the correct instance of the cell class.
This occurred because I had not correctly assigned this cell to be the delegate for the view controller. For anyone interested, in my screencast at 3:50, you can see that I have the following in cellForRowAtIndexPath:
RA_FormCell *cell = [tableView dequeueReusableCellWithIdentifier:self.formCellArray[indexPath.row] forIndexPath:indexPath];
self.delegate = cell
However, this means that self.delegate got continually overwritten as the table cells were generated. As a result, my controller:didUpdateDay message was sent to the bottom cell of the table, and not the top one as I required.
The solution was simple - there's no need to have this second delegate at all. Instead, when the cell delegates to the view controller, it should pass self into the message it delegates:
id<CustomCellDelegate> strongDelegate = self.delegate;
if ([strongDelegate respondsToSelector:#selector(customCell:didChangeDay1:)])
[strongDelegate customCell:self didChangeDay1:[sender value]];
Then, in the implementation of this method by the delegate, simply end it by changing the outlet directly:
-(void)customCell:(RA_FormCell *)customCell didChangeDay1:(double)value
// put logic here
customCell.dayOutlet.text = #"No problem!";
In general, there should rarely be a need for a two-way delegate structure. Keep it one way, from A to B, and just remember to have A pass self in any messages it sends to B. That way, B will know the object the message came from, and be able to communicate back to A.
Thanks to Paulw11

cell.accessoryType = UITableViewCellAccessoryDetailButton WHAT IS NEXT?

Still fairly new to iOS. I've managed to write a basic app to display a list/table of documents. I've included:
cell.accessoryType = UITableViewCellAccessoryDetailButton;
which display a blue i within a circle button which I guess can be used to perform some sort of action.
What I'd like to do now is, upon click, to display a subsequent screen with information about the document, buttons, add, delete functions, date, file size etc...
Is this done via segue or some other method?
Being a novice I am not sure what is/are the next step(s). If I know what steps I must take in order to get to next scree(s) I can search the net for example of how to do any given step.
Thank you for your help
The accessoryButton triggers it's own delegate method, distinct from the row selection, called:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
Add that method to your delegate, and then do a vc transition. There are several ways to do that transition, most of the common ones are discussed here...
First, welcome to the iOS developing community!
In order to do this, I would use a segue to a detail view that you can design. When you select a row in your table using didSelectRowAtIndexPath, you would set an #property to the object in your array that was selected: self.selectedObject = self.tableviewarray objectAtIndex:indexPath.row; Then in the prepareForSegue method you can get the destination view controller and do destinationViewController.myObject = self.selectedObject; Now the detail view knows what object to display info for!
You'll need to implement the delegate method
-tableView:(UITableView*) didSelectRowAtIndexPath:(NSIndexPath*)
and handle the row tap in that callback accordingly.
An example would be like this:
-tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
YourObject *theModelForRow = [_itemList objectAtIndex:indexPath.row];
YourViewController *someNewViewController = [YourViewController viewControllerWithModel:theModelForRow];
[self.navigationController pushViewController:someNewViewController animated:YES];
}

iOS 5, Storyboards,ARC: Creating and setting custom delegate and datasource to program. created UITableView

The prequel of this problem is here.
I am trying to create and set a custom delegate and datasource to my programmatically created UITableView. I've googled around, but couldn't find any clear solution for my problem.
Meanwhile, I've created a new class that conforms to UITableViewDelegate,UITableViewDataSource
protocols. In this class:
tableView numberOfRowsInSection: 20
tableView cellForRowAtIndexPath: cell.textLabel.text=#"Nominals";
Class that contains UIViews:
Method that creates UITableView:
-(IBAction)segmentValueChaged:(id)sender
{
if(self.segment.selectedSegmentIndex==0)
{
[self.coinageView removeFromSuperview];
[self.view addSubview:nominalsView];
[self populateNominals:self.subCountryID];
}
else
{
[self.nominalsView removeFromSuperview];
[self.view addSubview:coinageView];
[self populateCoinages:self.subCountryID];
}
}
-(void)populateNominals:(int)subCountryID
{
NominalsTableViewDelegate *del=[[NominalsTableViewDelegate alloc]init];
UITableView *nominalsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 372) style:UITableViewStylePlain];
[nominalsTableView setDelegate:del];
[nominalsTableView setDataSource:del];
[self.nominalsView addSubview:nominalsTableView];
[nominalsTableView reloadData];
}
Finally, I'm getting EXC_BAD_ACCESS.
The evil is in [nominalsTableView setDelegate:del]; [nominalsTableView setDataSource:del]; rows. What's wrong with them.
Help is much appreciated. Thanks in advance.
I am not sure if you are adding a new UITableView to your current view or not, but I will assume you are doing so.
If you have a class that conforms to the UITableViewDelegate. But when i needed to create a UITableView programmatically, I create a new UITableView class (with the .h and .m) then make a MutableArray and exposing it as a property to the parent view so it can set the data source.
From there, you can create an instance of the class along with the datasource (which you exposed from the child object). Finally, you then add the view onto your current view. This method you dont need to set the delegate because your child class conforms to the tableview delegate protocol.
If you are just modifying the data inside the current tableview then, you use a NSMutableArray and then change the data in it. After then do a
[self.tableView reloadData];
Hope this helps!
EDITED
I might have misunderstood your question, what you (most likely) need to do is create a property of the delegate class then create an instance of your delgate class and assign it to the property.
Then do a
[myTableView setDelegate:self.myProperty];
[myTableView setDatasource:self.myProperty];
This, I believe, would solve you bad access problem.
EDITED AGAIN
Create a property inside your .h of the tableview class as such:
#property (nonatomic, retain) NominalsTableViewDelegate *myDelegate;
From there then inside your .m file, you do something similar to this:
NominalsTableViewDelegate* delegateClass = [[NominalsTableViewDelegate alloc] init];
[self setMyDelegate:delegateClass];
[delegateClass release];
Then you can set your tableview datasource as such:
[myTableView setDelegate:self.myDelegate];
[myTableView setDatasource:self.myDelegate];
Note: I currently have no access to a machine to test this, but just something to point you towards.

One IBAction for multiple custom prototype cells with buttons

Using storyboard, I have a table view controller containing multiple dynamic prototype cells. One of the cells is a custom dynamic prototype cell (for which I created a subclass) containing a label and a switch. I have the action for the switch wired to an action method (say switchChanged:) in the view controller. In cellForRowAtIndexPath, I configure and return the cell appropriate for the specified index. So far so good.
The problem: my application has multiple instances of these custom cells, so how do I differentiate which switch has changed? One thought is that in the view controller I can create a member switch for each cell and link them to a specific cell switch when cellForRowAtIndexPath is called for the first time. Then I can use these member switches to compare with the switch that is passed into switchChanged:.
I know that I can try it and get an immediate answer, but I was hoping for a discussion of how other developers do what I am trying to do. Is this the best/worst/ok approach and what are practical alternatives?
Regards,
--John
I had this situation once (not with switches, but I believe it applies just the same). I've managed to get around it by subclassing the object class and adding the required properties/methods inside the subclass.
When calling the action, your sender will be the subclass, and you can access your added code there.
I don't know if it is the case, but if you're only trying to change a value, you should use bind the switch value to the property when creating the object. It will not even need an IBAction to call.
EDIT: Example:
#interface MySwitch : UISwitch
#property (nonatomic, assign) NSUInteger someProperty;
#end
Then, every time you create a cell, you can set "someProperty" to anything you want.
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
// yada yada yada...
UITableViewCell *cell;
// yada yada yada...
[cell.myLinkedSwitch setSomeProperty:indexPath.row];
return(cell);
}
Unless you're not creating your cells using the tableView:cellForRowAtIndexPath: method. Then you probably should use bindings to get your value to the right place.
Instead of adding a separate subclass, I just stored the row in each button Disabled Title property. This worked very will with little effort. This first code is in the CellForRowAtIndexPath:
NSString *strRow = [[NSString alloc] initWithFormat:#"%i",useRow];
[btnPreferredChk setTitle:strRow forState:UIControlStateDisabled];
Then my action method for the button uses that value to perform the appropriate activity.
- (IBAction)goStorePick:(id)sender
{
UIButton *useButton = [[UIButton alloc] init];
useButton = sender;
NSInteger *storeRow = [[useButton titleForState:UIControlStateDisabled] integerValue];
NSString *CMIMsg = [[NSString alloc] initWithFormat:#"goStorePick Method Executed at Row: %i", storeRow];
[self shwMessage:CMIMsg];
}
This worked well for me.

Resources