UIPickerView Spin Confusion - objective-c

I have an app that I am currently making that has a PickerView and a UIButton. I want the user to tap the button and make the picker view show a random selection. I have been looking around and can't seem to come up with anything. I guess it would be similar to the Urbanspoon app. I know it is just a piece of a code but I can't think of what it could be.

You are looking for – selectRow:inComponent:animated:
So do something like this:
[thePickerView selectRow:(arc4random() % numRows) inComponent:0 animated:YES];
where numRows is the number of rows. If the rows are associated with an array names rowArray for instance it would be:
[thePickerView selectRow:(arc4random() % [rowArray count]) inComponent:0 animated:YES];

So, upon pressing the button you would randomly choose an item from the data source of your PickerView and then call – selectRow:inComponent:animated: on your PickerView.

Related

OSX NSTableView insertRowAtIndexes

i already checked Using NSTableView insertRowsAtIndexes solution but It does not solve my problem.
i want insert row in nstableview at particular index(add dynamically)
index Set is valid, still it causes Program crash
NSIndexSet *indexSet=[NSIndexSet indexSetWithIndex:i];
[myTableView insertRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationEffectFade];
1)is any thing wrong in my code?
2)Is there any another way to add row at particular index(add dynamically)?
The code is correct but first you have to insert the model object in the data source array to keep model and view in sync.
I got My mistake..... problem In other Code so This code is fine
But I want to add some points About insertRowsAtIndexes: method
Hope it will helps to other people
1)Dont called reloadData() because you are adding particular number of rows so calling reloadData() will reload all data and it will causes crash
2) Calling this method multiple times within the same beginUpdates and endUpdates block is allowed, and changes are processed incrementally
3)Most Important thing is indexSet must be within range
if you are Enter valid indexSet then The numberOfRows in the table view is automatically increased by the count of indexes.
4)you can select animation according to your need
Sample Code :
[yourTableView beginUpdates];
NSIndexSet* theIndexSet = [NSIndexSet indexSetWithIndex:[self.yourTableContents count]-1];
[yourTableView insertRowsAtIndexes:theIndexSet withAnimation:NSTableViewAnimationEffectFade];
[yourTableView endUpdates];

How to reload a UItableView with rows and section

I have a UITableView displaying a list of plants in alphabetical sections. Within the app, the user can change the language. When the language is changed, the method:
[self.tableview reloadData];
now displays the same plants in a new language. The problem is that the rows and sections are intact, and still sorted in the alphabetical order of the previous language. I have googled and read up and down stackoverflow to find a solution. The closest I came was:
[[self tableView]reloadData];
NSRange range = NSMakeRange(0, [self numberOfSectionsInTableView:self.tableView]);
NSIndexSet *sections = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationFade];
Still, I have to run the tableView through viewdDidLoad to change the alphabetical sorting.
What is the best way to force a new alphabetial sorting after [[self tableview]reloadData]; ?
Before calling reloadData you first need to update all of your data used by the table view data source and delegate methods.
You do not need to call viewDidLoad again. Put the code to setup the data structures in its own method. Then you can call this setup method from viewDidLoad (if needed) as well as just before calling reloadData after a language change.

How do I move data from one array to another? Using UITableView

Following on from my last question, I have a tableview populated with 2 NSMutableArrays, 2 sections and 2 section headers, all of which are working ok.
incompleteItems = [NSMutableArray arrayWithObjects: #"Item 1", #"Item 2", #"Item 3"... , nil];
completeItems = [NSMutableArray arrayWithObjects: nil];
I have set it up so that when a user swipes to the right, the cell accessory changes from disclosure to checkmark, and when swiping to the left changes from checkmark to disclosure. The gestures are achieved using handleSwipeLeft/Right methods.
What I'd like to do is when a user swipes to the right, the Item from the incompleteItems array is moved into the completedItems array, thus moving the item from the "Incomplete" section to the "Complete" section.
What would be the best way to achieve such a thing? My experience is limited so this is somewhat outside of my knowledge.
Any help appreciated as always.
You can simply add the object from one array to the other and then delete it immediately.
-(void)incompleteToCompleteAtIndex:(NSUInteger)index
{
[complete addObject:[incompleteItems objectAtIndex:index]];
[incompleteItems removeObjectAtIndex:index];
}
-(void)completeToIncompleteAtIndex:(NSUInteger)index
{
[incompleteItems addObject:[complete objectAtIndex:index]];
[complete removeObjectAtIndex:index];
}
Since this will only occur as a result of a UI action I assume that this will happen on the main thread and there will be no reason for locking.
Do you need a nice animation? Check out: deleteRowsAtIndexPaths:withRowAnimation: and insertRowsAtIndexPaths:withRowAnimation:. Make sure you wrap these calls with beginUpdates and endUpdates.
I don't know of an easy way to animate moving a cell from one tableView to another. I can imagine a method that requires you to do a 3-step animation manually, removing the cell (and adding it to a new view that floats in front of your whole view hierarchy), moving the new view to the destination location (you may need to do your own math to determine the screen coordinates of the destination) and finally an addition animation. You could use a dummy cell that is completely transparent and swap it in / out in the first and last pieces of the animation to give the tableview something to hold the place of your transferred cell.

Table view: disable a row or make an alert which stop a segue?

I have two table view filled in their viewDidLoad with data coming from a sqllite db containing names of shops. In the first view i choose one element (i.e. one shop), then in the second i have to choose another shop different from the first.
I can disable one of the row of the second table view after it has been filled?
I tried not to load the name (of the first shop choosen) in the second view but i fill the table view in its viewDidLoad and unfortunately the data regarding the name of the first shop choosen doesn't seem to be available until the viewDidAppear and that moment is too late to fill the table view.
I tried, also, to add an alert to the event of choosing one row in the second view in this way
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *choosenSecondShop= [secondShop objectAtIndex:indexPath.row];
NSString* nameFirstShop = [function loadFirstShopChoosen];
if ([choosenSecondShop isEqualToString:nameFirstShop]) {
NSString *message=#"Second shop cannot match the first";
alert = [[UIAlertView alloc] initWithTitle:#"Errore"
message: message
delegate: self cancelButtonTitle: #"Ok"
otherButtonTitles:nil,
nil];
[alert show];
}else {
// go on with the app's stuff!
}
}
but in this case the alert is displayed but also the segue followed. I can prevent the segue to go on?
There are a few different ways to attack this problem:
1. Conditional segue
This should work, though I'm not sure it's a great idea from a user experience perspective.
Presumably you made a segue in IB starting from the table cell? When you do that, you get the convenience of not needing any code to perform the segue (it's automatically done when you tap the table cell), but you don't get any run-time control over it either.
If you need programmatic control over whether/when/which segue to perform, you should create a segue starting from the view controller itself (not from a control within it), and give the segue a unique identifier in IB. Then, in your tableView:didSelectRowAtIndexPath: implementation, you can call [self performSegueWithIdentifier:#"myIdentifier"] once you know you want to perform the segue.
2. Disable cell selection
To prevent a specific cell from being selected, your table view controller can implement tableView:willSelectRowAtIndexPath: to return nil for any index path you don't want the user to select.
If you do that, you might want to make it clear to the user which rows can be selected -- you can alter the cell's appearance in tableView:cellForRowAtIndexPath:.
3. Don't show the cell
You say you're populating the table view in viewDidLoad, but it doesn't really work that way: Table views populate themselves by calling your data source & delegate methods when they need to. If you want to prevent an item from your data set from being shown as a cell in the table, you just need to alter the behavior of your data source & delegate methods:
tableView:numberOfRowsInSection: should return a number one less than it would otherwise
tableView:cellForRowAtIndexPath: should reflect the removal of the item; something like:
MyShop *shop;
if (indexPath.row < indexOfFirstShop)
shop = [shops objectAtIndex:indexPath.row];
else
shop = [shops objectAtIndex:(indexPath.row + 1)];
// then configure cell for the chosen shop
Now, if you don't know which row you want to hide as of the first time these methods are called, all you need to do once you get that information is tell the table view that it needs to call them again: [self.tableView reloadData] should do the trick.

reloadData calls numberOfSections, numberOfRows, not cellForRowAtIndexPath

first of all sorry if this isn't formatted correctly, first time doing this. I've been using stackoverflow to find help for a long time now and it's been very helpful (thank you all), but this is the first time I've posted a question of my own. This question has been asked many times, but when I call [myTable reloadTable] the methods numberOfSectionsInTableView and numberOfRowsInSection are called but not cellForRowAtIndexPath.
Every answer I've seen when searching has been a variation of:
1) The tableView is nil
2) numberOfRowsInSection is 0
3) tableView's delegate/data source not set
4) calling reloadTable on the wrong uiTableView.
None of these are the case for me so I'm wondering what else could be wrong.
What I'm trying to do:
I have a custom uitableviewcell with a button on it, when the button is pressed I want to change the attributes of the cell (for an example, let's just say I want to change the cell title to "Button Pressed"). I then want to pause for a moment and then delete the cell.
Not sure if this is important, but my tableView is inside a UIViewController, and the viewController is the delegate and dataSource.
I have a method (below) that fires when the button is pressed and the cell attributes are changed as necessary, however when I try to refresh the table to show the changes it doesn't work. The first time [remTable reloadData] is called, numberofsectionsintableview and numberofrowsinsection are called but not cellforrowatindexpath. However, the second time [remTable reloadData] is called all three methods are called and everything works correctly.
doneCell.remName.text=#"BUTTON PRESSED";
[remTable reloadData];
NSLog(#"sleep");
sleep(1);
[remList removeObject:doneReminder];
[remTable reloadData];
To test this I put nslog statements at the beginning of numberofsections, numberofrows and cellforrow, the output is the name of the method followed by the number (for numberofsections/numberofrows).
Output:
numberofsections 1
numberofrows 3
sleep
numberofsections 1
numberofrows 2
cellforrow
Any ideas as to why cellforrow's not being called the first time? Thanks in advance, please let me know if there's anything else I can add to clarify.
A table view doesn't actually request its cells until it needs to display them. When you call reloadData, it will immediately request the number of sections and rows so that it knows how big it needs to be. It doesn't ask for the cells themselves until it is asked to display itself. Views are automatically displayed as necessary at the beginning of each run loop.
Execution doesn't return to the run loop until your code returns. When you call sleep, you don't execute any code, but you also don't return. This means the run loop doesn't get a chance to display the table, so it never asks for any cells. What you need to do is return from your method and ask that another method gets called in 1 second to remove the doneReminder. An easy way to do this is to use the performSelector:withObject:afterDelay: method. This method adds a timer which will call the method you requested after the given delay, which means you can return to the run loop and let the table display.
doneCell.remName.text=#"BUTTON PRESSED";
[remTable reloadData];
[self performSelector:#selector(removeDoneReminder) withObject:nil afterDelay:1.0];
- (void)removeDoneReminder {
[remList removeObject:doneReminder];
[remTable reloadData];
}
If remlist and remTable are not instance variables, you can use the withObject: parameter to send them to the removeDoneReminder method.
CellForRowAtIndexPath is part of UITableView datasource protocol, make sure that remtable.datasource is point to self
so when you initialize the TableView, you should add
remtable.datasource = self;
i always did this, after
remtable.delegate = self;
Hope this help
Try using delayed performance:
[remtable performSelector:#selector(reloadData) withObject:nil afterDelay:0.1];
Don't know if it will help because I don't know what else you're doing.