indexpath as parameters - objective-c

I got an table which is filled with VM's and i have 2 variable of the datatype NSIndexPath called:
selectedindexpath(the row the user clicks on) and
selectedindexpathFortheVMtoTurnOn(so it knows which row to reload when the VM went on);
which is both made in the .h as #paramters(retain) ..
i got a function that turns on the VM's and a function that CHECKS every 1 sec with a NSTimer if the guest is finally on then it reloads the row of selectedindexpathFortheVMtoTurnOn
im doing the same for OFF/REBOOT but i have got a problem with this.. im not able to request an action for more then 1 VM.. because it is overwriting the selectedindexpathFortheVMtoTurnOn value with the last row which i sent an action to and by doing so the table responds wierd and the app crashes..
so thats why i want to give the indexpath.row value as parameters with the NSTimer to the function that checks every 1 sec i have tried a few things but none have worked.. below the code of how i am giving the parameters
timertocallguest=[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(gettheguestyouaskedforTOTURNON:)
userInfo:selectedindexpathFortheVMtoTurnOn
repeats:YES];
and the function:
-(void)gettheguestyouaskedforTOTURNON:(NSIndexPath *)therow
and when i try to do a NSLog("%d",therow.row); it crashes...
and with NSLog("%d",therow); i get a whole diffrent value of the selectedindexpathFortheVMtoTurnOn that it used to had..
what am i doing wrong.?
sorry if its a wall of text. but i realy need to fix this problem as this is getting released as BETA nextweek in the appstore for the company where my traineeperiode is.
ty in advance.
EDIT:
i will try to show it with pictures this time.
At first im at the view with VM's
http://imageshack.us/photo/my-images/194/schermafbeelding2011052l.png/
after that i can press on the button with the arrow and i get to choose what i want to do.
http://imageshack.us/photo/my-images/822/schermafbeelding2011052r.png/
an activity indicator appears and it stops when the action is done.(which has to do with the NStimer that checks every 1 sec)
http://imageshack.us/photo/my-images/828/schermafbeelding2011052y.png/
http://imageshack.us/photo/my-images/811/schermafbeelding2011052t.png/
but when i try to do an action at 2 or more VM's the activity indicator on the last row i selected spins *x faster(depends on how manny actions i send.. 2 actions means it spins 2 times faster..(so weird)
.. i hope this is enough for you to understand what i mean now =)

The method triggered by an NSTimer object always has the timer as the argument. You can get the row like this.
-(void)gettheguestyouaskedforTOTURNON:(NSTimer *)timer {
NSIndexPath *indexPath = (NSIndexPath*)[timer userInfo];
NSLog(#"%d", indexPath.row);
}

Related

Incorrect indexPath against UITableView after calling reloadData from textFieldDidEndEditing

So I'm trying to emulate the way in which the standard Apple code works on the contacts app when managing telephone numbers. Specifically here, I'm working on deleting a row from a tableview if its empty and the user navigates to any other row
My problem is that as the tableview reload causes the UITextField to resign its responder, I need to set the responder again for the text field navigated to by the user
I have the UITextField delegate and am handling the usual textFieldShouldBeginEditing , textFieldDidBeginEditing , textFieldShouldEndEditing , textFieldDidEndEditing
In order to handle the functionality, my code is within textFieldDidEndEditing, whereby I remove the data from the tableview array, and as the tableview has 2 sections, I am calling:
[MyTableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
During textFieldDidBeginEditing I save the indexPath of the textField being edited using:
EditableCustomCell *textFieldCell = (EditableCustomCell *)[[textField superview] superview];
NSIndexPath *indexPath = [MyTableView indexPathForCell:(EditableCustomCell *)textFieldCell];
responderIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
I then use the following code to set the correct rows textField to be the first responder:
EditableCustomCell *customCell = (EditableCustomCell *)[MyTableView cellForRowAtIndexPath:responderIndexPath];
[customCell.editableTextField becomeFirstResponder];
Everything seems fine until near the end of the processing, when all of a sudden textFieldDidBeginEditing starts to return section 0 row 0 for the indexPath (even though when examining the tag value or text contained return the correct values for the textfield)
Below is a log from the start of the process explained above:
- textFieldDidEndEditing started <-- start of delete processing
- textFieldDidEndEditing - tableviewData - replacing object at index 1
CustomTableViewController.deleteRow - delete called for indexPath section 1 ... row 1
- reloading MyTableView
- CustomTableViewController-cellForRowAtIndexPath started
CustomTableViewController-cellForRowAtIndexPath section=1 row=0
CustomTableViewController-cellForRowAtIndexPath ending
CustomTableViewController-cellForRowAtIndexPath section=1 row=1
CustomTableViewController-cellForRowAtIndexPath ending
CustomTableViewController-cellForRowAtIndexPath section=1 row=2
CustomTableViewController-cellForRowAtIndexPath ending
- textFieldShouldBeginEditing started
indexPath for textFieldShouldBeginEditing is : section 1 row 1
- textFieldShouldBeginEditing ending
- textFieldDidBeginEditing started
indexPath for textFieldDidBeginEditing is : section 1 row 1 text 3 tag 1
- textFieldDidBeginEditing ending
- textFieldDidEndEditing ending <-- end of delete processing
- textFieldDidBeginEditing started
- textFieldDidBeginEditing ... setting responderIndexPath section 0 row 0
indexPath for textFieldDidBeginEditing is : section 0 row 0 text 123 tag 0
- textFieldDidBeginEditing ending
As can be seen from the last part of the log, after textFieldDidEndEditing completes, textFieldDidBeginEditing is called but returns section 0 and row 0 (the rows stay displayed and visible throughout)
I can neither understand why this is called, or why it is not returning the correct indexPath. As can be seen, the text is returned for the value (in this case the entered value of 123) and I have verified this with other data and other rows (for both text and tag for the textfield)
Perhaps my setting of becomeFirstReponsder within textFieldDidEndEditing is incorrect, although if that's true, I'm at a loss as to where to process this
Hoping someone out there with a better understanding of this can help me out, as you can tell, I've been through this for several hours without any resolution
Thanks
Izzy
EDIT 1: In the code, all that is called is becomeFirstReponder before textFieldDidEndEditing completes. When you look at the log after cellForRowAtIndexPath has completed, it enters and exists the textfield TWICE, once for the row that was below the row just removed, and then again when it returns 0 for the section/row of the indexPath ? I am failing to understand what sequence of events is causing this post table reload firing of methods
EDIT 2: Is it just me, or does it seem really strange that there is NO textfieldSHOULDbeginEditing prior to the textFieldDidBeginEditing at the end of the log ? Is it that I am screwing with the internal process by reloading the table during textFieldDidEndEditing ? Is there a better place to do this (ie. remove a row and reload the tableview to show the UI update) ?
OK, to answer my own question ...
After alot of debugging, it seems that ...
I'm inteferring with the standard UI workflow by reloading the tableview during textfieldDidEndEditing
Calling reloaddata destroys the tableview cells (and all objects contained within the cells ie. UITextFields, UILabels, etc) and recreates them, causing
By moving this call out to a standalone button, it functioned as expected without the indexPath returning 0 (I still dont fully understand the call stack when it gets to this point, but hey)
Now my BIG problem is deciding when to programatically call the reload, as I want it driven off the event of leaving the text field ... I feel another question coming on :\
Hope my ramblings help someone out in the future that tries to do a similar thing ...

Objective-C iOS, set a delay for an update of text

I'm trying to find a solution to a problem I just stumbled upon.
Tried to search for it but it's not that I'm looking for.
I'm doing a GPS-app with two tabbars. I track the distance in the map-view (using CLLocation) and when I change tab to a different view the stringtext that say what distance it is from when I started don't update immediately, it take a couple of seconds.
And when I press the stop button I want it to either wait those couple of seconds so the real distance updates. But I dont want to freeze the app.
(*NSDate future = [NSDate dateWithTimeIntervalSinceNow: 3.0 ];
[NSThread sleepUntilDate:future];)
I'm saving the string with the distance in a cell in the second tab, so if I multitask while the app is going I want to just start the app and press stop. And then the new distance will be correct and saved. Hope I didn't confuse you with this much text I hope you understand what I asking for!
thx
[label performSelector:#selector(setText:) withObject:newText afterDelay:3.0];
So what you want to do is use dispatch_after:
dispatch_after(3 seconds, dispatch_get_main_queue(), ^
{
myLabel.text = <the value you want to appear>;
} );

Adjust a "for" loops duration

I'm currently working on an app that will show a label that will start at zero, and count up to a number I specify. I wanted to do this using a simple loop like this one.
for (counterInt = 0; counterInt < 10; counterInt++)
{
NSLog(#"%i",counterInt);
}
The problem is, that this loop executes in less time than it takes for the view to appear on screen. My console logs 1-9 before the view finally loads with the label with showing 9. I've been researching for several hours trying to find a way to specify a duration for the loop and I can't seem to find any thing on this.
So my overall question is, is it possible to specify how long the loop should take to execute? If so, if anyone can point me in the right direction here it would be greatly appreciated!
Even if you slowed down the loop, it still wouldn't work. UI elements are only updated at the end of the run loop. You need to set up a timer and update the label in the method fired by the timer.
You're using the wrong approach. Try an NSTimer.
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(incrementLabel:)
userInfo:nil
repeats:YES];
Then create a method called incrementLabel and have it increment an instance variable and update the label accordingly.
You can sleep a thread:
[NSThread sleepForTimeInterval:1];
However, jrturton and james supply the correct approach. I would place James' code in the viewDidAppear method so your label starts where you want it and the counter begins when the view appears.

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.

Eternal scrolling UITableView

I'm trying to create a UITableView with dates, shouldn't be very exciting, I know. It starts around the current date, but the user should be able to scroll down (the future) and up (the past) as far as he/she wants. This results in a potentially infinite number of rows. So what's the way to go about creating this?
Returning NSIntegerMax as the number of rows already crashes the app, but even if it wouldn't, that still doesn't account for being able to scroll up. I could start half way of course, but eventually there's a maximum.
Any ideas how to do or fake this? Can I update/reload the table somehow without the user noticing, so I never run into a border?
SOLUTION:
I went with #ender's suggestion and made a table with a fixed amount of cells. But instead of reloading it when the user scrolls to near the edges of the fixed cells, I went with reloading the table when the scrolling grinds to a halt. To accomodate with a user scrolling great distances without stopping, I just increased the row count to 1000, putting the ROW_CENTER constant to 500. This is the method that takes care of updating the rows.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSArray *visible = [self.tableView indexPathsForVisibleRows];
NSIndexPath *upper = [visible objectAtIndex:0];
NSIndexPath *lower = [visible lastObject];
// adjust the table to compensate for the up- or downward scrolling
NSInteger upperDiff = ROW_CENTER - upper.row;
NSInteger lowerDiff = lower.row - ROW_CENTER;
// the greater difference marks the direction we need to compensate
NSInteger magnitude = (lowerDiff > upperDiff) ? lowerDiff : -upperDiff;
self.offset += magnitude;
CGFloat height = [self tableView:self.tableView heightForRowAtIndexPath:lower];
CGPoint current = self.tableView.contentOffset;
current.y -= magnitude * height;
[self.tableView setContentOffset:current animated:NO];
NSIndexPath *selection = [self.tableView indexPathForSelectedRow];
[self.tableView reloadData];
if (selection)
{
// reselect a prior selected cell after the reload.
selection = [NSIndexPath indexPathForRow:selection.row - magnitude inSection:selection.section];
[self.tableView selectRowAtIndexPath:selection animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
The magic breaks when a user scrolls to the edge of the table without stopping, but with the table view bounces property disabled, this merely feels like a minor glitch, yet totally acceptable. As always, thanks StackOverflow!
You should establish a fixed number of cells and adjust your datasource when the user scrolls near the end of the tableview. For example, you have an array with 51 dates (today, 25 future and 25 past). When the app tries to render a cell near one of the borders, reconfigure your array and call reloadData
You might also have a look at the "Advanced Scroll View Techniques" talk of the WWDC 2011. They showed how you would create a UIScrollView which scrolls indefinitely. It starts at about 5 mins. in.
Two thoughts:
Do you really need a UITableView? You could use a UIScrollView, three screens high. If end of scrolling is reached, layout your content and adjust scrolling position. This gives the illusion of infinite scrolling. Creating some date labels and arranging them in layoutSubviews: should not be too much of an effort.
If you really want to stick to UITableView you could think about having two UITableViews. If scrolling in your first one reaches a critical point, spin off a thread and populate the second one. Then at some point, exchange the views and trigger the scrolling manually so that the user does not note the change. This is just some idea from the top of my head. I have not implemented something like this yet, but I implemented the infinite UIScrollView.
I answered this question in another post: https://stackoverflow.com/a/15305937/2030823
Include:
https://github.com/samvermette/SVPullToRefresh
SVPullToRefresh handles the logic when UITableView reaches the bottom. A spinner is shown automatically and a callback block is fired. You add in your business logic to the callback block.
#import "UIScrollView+SVInfiniteScrolling.h"
// ...
[tableView addInfiniteScrollingWithActionHandler:^{
// append data to data source, insert new cells at the end of table view
// call [tableView.infiniteScrollingView stopAnimating] when done
}];
This question has already been asked: implementing a cyclic UITableView
I'm copying that answer here to make it easier because the asker hasn't ticked my answer.
UITableView is same as UIScrollView in scrollViewDidScroll method.
So, its easy to emulate infinite scrolling.
double the array so that head and tail are joined together to emulate circular table
use my following code to make user switch between 1st part of doubled table and 2nd part of doubled table when they tend to reach the start or the end of the table.
:
/* To emulate infinite scrolling...
The table data was doubled to join the head and tail: (suppose table had 1,2,3,4)
1 2 3 4|1 2 3 4 (actual data doubled)
---------------
1 2 3 4 5 6 7 8 (visualizing joined table in eight parts)
When the user scrolls backwards to 1/8th of the joined table, user is actually at the 1/4th of actual data, so we scroll instantly (we take user) to the 5/8th of the joined table where the cells are exactly the same.
Similarly, when user scrolls to 6/8th of the table, we will scroll back to 2/8th where the cells are same. (I'm using 6/8th when 7/8th sound more logical because 6/8th is good for small tables.)
In simple words, when user reaches 1/4th of the first half of table, we scroll to 1/4th of the second half, when he reaches 2/4th of the second half of table, we scroll to the 2/4 of first half. This is done simply by subtracting OR adding half the length of the new/joined table.
Written and posted by Anup Kattel. Feel free to use this code. Please keep these comments if you don't mind.
*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView_
{
CGFloat currentOffsetX = scrollView_.contentOffset.x;
CGFloat currentOffSetY = scrollView_.contentOffset.y;
CGFloat contentHeight = scrollView_.contentSize.height;
if (currentOffSetY < (contentHeight / 8.0)) {
scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
}
if (currentOffSetY > ((contentHeight * 6)/ 8.0)) {
scrollView_.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
}
}
P.S. - I've used this code on one of my apps called NT Time Table (Lite). If you want the preview, you can check out the app: https://itunes.apple.com/au/app/nt-time-table-lite/id528213278?mt=8
If your table can sometimes be too short, at the beginning of the above method you can add a if logic to exit when data count is say for example less than 9.