I want that ,if the users taps a button, these tableCell should be added to a new tableView (where you can see your favorites).
I've one class Car where the content of the cars is synthesized:
#import "Car.h"
#implementation Car
#synthesize name;
#synthesize speed;
#synthesize selected;
#end
The CarsViewController contains a tableView where you can find each car:
#implementation CarsViewController {
NSArray *cars;
}
#synthesize tableView = _tableView;
- (void)viewDidLoad
{
Car *car1 = [Car new];
car1.name = #"A1";
car1.speed = #"200 km/h";
car1.selected = FALSE;
Car *car2 = [Car new];
car2.name = #"A2";
car2.speed = #"220 km/h";
car2.selected = FALSE;
cars = [NSArray arrayWithObjects:car1, car2, nil];
}
And finally the CarDetailViewController has the favorite-button.
And if you click on it the cell of these car should be added to a new ViewController (with TableView).
But what should I add to the method of CarDetailViewController?
- (IBAction)setFAV:(id)sender {
// don't know what to write here
}
Thanks in advance for all your answers.
UPDATE
So here is a little picture which shows how it should work.
In this method you need an array which hold all selected cars and u need to pass this array to newView controller there u use this array to show fav cars
Make your car array a NSMutableArray or create a new NSMutableArray with all your favourite cars and if the user clicks the button, add a new object to that array.
Then, this array should be the data source of your tableview and after adding a new object to the NSMutableArray do
[tableview reloadData];
Your Car class could have a BOOL isFavorite; and then on a selection in the first tableView you could set the isFavorite property for the car at the index selected to YES. Then, in your favorite table view, one option is to show only those cars in your list who have the isFavorite = YES.
There are many ways to do this, and since you haven't specified when or where you're going to be using your Favorites Table View, one solution that might work for you is to use NSUserDefaults.
When the user hits the favorite button (Assuming you've already created and initialized an array)
[favoritesArray addObject:Car1];
[[NSUserDefaults standardUserDefaults] setObject:favoritesArray forKey:#"favoriteCars"];
And then when you need to access this array to use as the data source for your Favorites TableView in another view controller:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"favoriteCars"]];
Related
Problem :
I develop Quiz apps. In the apps there are like 5 questions in one tableview. Questions are taken from a plist file. Whenever I have selected the questions choice in the tableview it is showing the current selected index_path value.
But I want all the question choices like from 1 to 5 selected index_path in the array.
My Code :
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// what is that code,....?
}
I want:
NSLog(#"whole index_path : %#",array name);
output:
whole index_path :
0
1
0
2
1
ok. you have to do three things:
go to your storyboard and give the segue from the resultsviewcontroller to the analysisviewcontroller an identifier "AnalysisScreen"
go to your resultsviewcontroller.m and implement the following method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"AnalysisScreen"]) {
AnalysisViewController *analysisViewController = segue.destinationViewController;
analysisViewController.currentQuiz = self.currentQuiz;
}
}
go to your analysisviewcontroller.m and in cellForItemAtIndexPath replace the line
Quizcell.TotalQuestions.backgroundColor = [UIColor whiteColor];
with the following lines:
Question *question = self.currentQuiz[indexPath.row];
Quizcell.TotalQuestions.backgroundColor = (question.selectedResponse == question.correctResponse) ? [UIColor greenColor] : [UIColor redColor];
hope i got you right! :)
Create an NSMutableArray and add index paths as objects. Whenever you are showing a question like if it's first question access its object from array and see which index path is selected and set that row as selected, and similarly for other questions.
But I would suggest you that rather than keeping index paths in array create a NSObject class with question id, selected answer id as members and when you are showing a question for which the user has selected something earlier than access the selected answer id and show that as selected.
Suppose you have 5 questions, and each question can have max 5 options.
You have a screen with a table view, you show 1 question with there options (2 - 5 rows) in table view at a time, you would have some option for the user to navigate to next questions (in that case you reload the table with next question)
This is your set up.
Now, as per my suggestion create a NSObject class to keep information for the selected option for each question.
#interface CustomObject : NSObject
{
NSInteger _questionId;
NSInteger _answerId;
}
#property (nonatomic,readwrite,assign) NSInteger questionId;
#property (nonatomic,readwrite,assign) NSInteger answerId;
#end
#import "CustomObject.h”
#implementation CustomObject
#synthesize questionId = _questionId, answerId = _answerId;
-(id) initWithQuestionId:(NSUInteger)qId answerId:(NSUInteger)aId
{
if( (self=[super init]) ) {
_questionId = qId;
_answerId = aId;
}
return self;
}
#end
Now suppose for the 5 questions you have the id’s as 1, 2, 3, 4, 5 and initially no answer is selected so keep a non applicable id for selected answer id as -1.
Create a NSMutableArray inside your class showing table view as
NSMutableArray *m_selectedAnsInfoList;
initialize above array once as
m_selectedAnsInfoList = [NSMuatbleArray alloc] init];
initially create object of CustomObject class for each question as
(suppose we have 5 question so questionCount = 5)
for (int i=1; i <= questionCount : i++)// will iterate for 5 times
{
//create instance of CustomObject with current iteration count as question id and
// initially -1 for answer id, as for the first time no answer will be selected
CustomObject *obj = [[CustomObject alloc] initWithQuestionId:i answerId:-1];
// add object to the array
[m_selectedAnsInfoList addObject:obj];
[obj release];
}
You create table view with showing answers as rows for a question, initially you will show all the rows as unselected.
inside didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// currentQuestionId keeps id of the table view showing current question
// using which access the questionInfoObject
obj = [m_selectedAnsInfoList objectAtIndex:currentQuestionId];
// set answer id in object
obj.answerId = indexPath.row; (from 0 to (number of answers - 1));
// replace old object with updated obj in array as
[m_selectedAnsInfoList replaceObject:obj atIndex:currentQuestionId];
}
In this way you store the information of the answers selected for each question and show them selected based on the information stored in the corresponding object in list.
I want to use the same ViewController that upon click on a button changes "data" on the display. It's going to be used for a quiz app, so i want the view controller for the questions to run the same logic but with (of course) different questions and answers when the user selects an answer. I read some about views and subviews and also about the viewDidLoad function, but i can't seem to find "best practices" in this matter.
Thankful for any help!
Olof
You can do this: create a model of your data and put a list of model items in a list. When the user clicks on the button, update your UI elements with the next item in the list. Here it is some code to think about:
#implementation YourViewController {
NSArray *items;
NSUInteger itemCount;
}
- (void)viewDidLoad {
itemCount = 0;
// load your items
}
- (IBAction)nextButtonTapped:(UIBarButtonItem *)sender {
itemCount++;
// check array boundaries
// update UI
}
#end
Complicated wording for something that is relatively simple. Where is an array of pictures defined if, it itself is a particular array item?
I have a set of arrays which are passed through a tableView to a DetailedViewController. One of which is images. On this DetailedView I would like to be able to implement a swipe gesture on a UIImageView so I can swipe through what essentially is an array of images inside of a particular array item.
PseudoCode:
Array 1:
array1-string1
array1-string2
array1-string3
Array 2:
array2-string1
array2-string2
array3-string3
Array 3:
array3-picture1
array3-picture2
array3-picture3
Each array stores a different set of data corresponding to the position in the array. eg: Array1 is all the names, Array2 is all the descriptions, Array3 is all the pictures.
The detailedView displays one set of data. eg. array1-string1, array2-string1, array3-picture1.
Correct me if i'm wrong but in order to use the swipe gesture (for a particular set of data) I need an array of images as the array3-picture1 item.
Would I specify this array when I initially declare all the arrays, in order for it to be hooked to an IBAction on the final detailedViewController, keeping in mind that this is called on a specific segue to the tableView.
OR
Simply declare the array as just an array at the start and specify the values of it somewhere else.
Sorry if that makes no sense, but dealing with this has been doing my head in!
I'd begin by considering this input data as not yet in object form. You can create an NSObject subclass or a dictionary grouping related data. For example (using dictionary):
NSMutableArray *objects = [NSMutableArray array];
NSAssert(array1.count == array2.count == array3.count, #"arrays must be of equal length");
NSInteger count = array1.count;
for (NSInteger i=0; i<count; i++) {
NSDictionary *d = #{ #"name": array1[i], #"description": array2[i], #"image": array3[i] };
[objects addObject:d];
}
This objects array can be the model for your table view, maybe presenting the names at the top level. Upon selection, segue to a detail view, passing data like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"DetailSegue"]) {
MyDetailViewController *vc = segue.destinationViewController;
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
vc.object = self.objects[selectedIndexPath.row];
}
}
Then the detail vc does it's business by referring to the public property which I assumed in the above to be called object. e.g.:
self.descriptionLabel.text = self.object[#"description"];
I am trying to pass an array from one table ,and populate it in another table.The parent table is place upon a UINavigationController say "mainNavig".The child table is placed in another ViewController of name "SongsofAlbum".My didSelectRowAtIndexPath of parent table is as follows,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
albumName = [eliminateDupe objectAtIndex:indexPath.row];
temp = [dictforalbum allKeysForObject:albumName ];
songs = [NSMutableArray arrayWithCapacity:[temp count]];
for (NSString *filename in temp) {
[songs addObject:[[filename lastPathComponent] stringByDeletingPathExtension]];
}
NSLog(#"songs are %#",songs);
songObj = [[SongsofAlbum alloc]initWithNibName:#"SongsofAlbum" bundle:nil];
[mainNavig pushViewController:songObj animated:YES];
songObj.albumname = albumName;
songObj.songArray = songs;
NSLog(#"the song object array is %# ",songObj.songArray)
}
The nslog of songObj.songArray returns the data in the above method .But the problem I face is ,when I call this songArray in the child view controller it returns NULL . I even property synthesized the arrays. Any suggestions?
But the problem I face is ,when I call this songArray in the child
view controller it returns NULL . I even property synthesized the
arrays.
In other ViewController you are creating a new instance of this class. The new instance will be NULL.
You do not need to create new instance infact you need to use this same object's value from there.
You can do this thing by: How to make button in child view to update information in its parent view?
You can take your songs array in AppDelegate and then instead assigning songObj.songArray = songs you can assign it in your SongsofAlbum class's viewDidLoad like
AppDelegate *app;
- (void)viewDidLoad
{
[super viewDidLoad];
app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
songarray= [[NSMutableArray alloc]initWithArray:app.songs];
}
Hope this will work.
I'm trying to build an application that uses something like the following data model. I can do this fine in a tableView but I would like to be more creative. For Example, Inside a navigation controller, I want to have a view that has 10 images (or buttons with an image as a background).
(three entities)
entity 1: House
attribute: houseName
relationship:person
entity 2:Person
attribute: personName
relationship:house
relationship:children
entity 3:Children
attribute:name
attribute:birthPlace
relationship: adult
relationships are one to many down from
attributes are all strings
person<---->>house, children<--->>adult
In the first view there are images and each image is assigned to entity House (houseName 1, houseName 2, etc.). When you select a house it pushes the next view with 5 images (that is linked to the Person Entity (personName 1, personName2, etc). When you select the PersonName it will push the next view populated with the Children Entity.
I've read quite a bit of info on core data and I'm comfortable doing this:
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [[managedObject valueForKey#"houseName"] description];
but very unsure where I should even start with using and image or button to do this
I was thinking of something like this:
-(id)initWithHouseViewController:(HouseViewController*)aHouseViewController house:(NSManagedObject *)aHouse{
if blah blah
self.houseViewController = aHouseViewController;
self.house = aHouse
}return self;
}
// for a selector
-(void) showPersonView{
PersonViewController *pvc = [[PersonViewController alloc] initWithHouseViewController:self house:house];
in the viewDidLoad
{
if (house != nil) {
UIImageView *house1 = [[ UIImageView alloc.. blah blah
some kind of.. action:#selector(showPersonView)
((for each houseName in house) instead of house1, house2,)
...
}
Any suggestions with the viewDidLoad where I wouldn't need to hard code in each image (or button) would be great to make this more portable. Also, I'm not rooted to doing it this way if there are suggestions as to a nicer/more creative way to do this.
Sorry if this is a bit messy. It's messy in my mind as well.
Thanks for taking your time to read this,
I wouldn't use FetchResultController in this case.
Instead You can try This:
Create a view controller that will show all the houses as buttons, you can dynamically create the buttons according to the "Houses" count.
It will look something like this:
NSArray * allHouses = [Houses allObjects];
//you can sort them if you need
[allHouses enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//create the buttons as custom and you can add any image to the button
UIButton *button....
button
//store the house index as the button tag, so you can get it later
button.tag = idx;
//add a selector to the button
[button addTarget:self
action:#selector(getPersons:)
forControlEvents:UIControlEventTouchUpInside];
}];
Create a new PersonViewController that will hold the persons. the view controller should get the house as the parameter and not the person. After it gets the house you will get the persons with the relationShip.
Now add this method to get the :
-(void)getPersons(UIButton*)sender{
NSInteger *tag = sender.tag;
House * house = (House*)[allHouses objectAtIndex:tag];
PersonViewController *pvc = [[PersonViewController alloc] init];
pvc.house = house;
[self.navigationController pushViewController.....];
}
Inside the person view controller you can get all the persons or with fetch request or simply with :
NSArray *allPersons = [house.person all objects];
Again create the persons buttons the same as you did with the first view controller.
Reapit the same procedure with the Children view controller.
GoodLuck
Shani