I can successfully push only next view in my iPhone app. However, cause the next view retrieves data to populate the UITableViews, sometimes waiting time could be a few seconds or slightly longer depending on connection.
During this time, the user may think the app has frozen etc. So, to counter this problem, I think implementing UIActivityIndicators would be a good way to let the user know that the app is working.
Can someone tell me where I could implement this?
Thanks.
pushDetailView Method
- (void)pushDetailView {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//load the clicked cell.
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath];
//init the controller.
AlertsDetailsView *controller = nil;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView_iPad" bundle:nil];
} else {
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView" bundle:nil];
}
//set the ID and call JSON in the controller.
[controller setID:[cell getID]];
//show the view.
[self.navigationController pushViewController:controller animated:YES];
You can implement this in didSelectRowAtIndexPath: method itself.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
This will show the activity indicator at the right side of the cell which will make the user feel that something is loading.
Edit: If you set the accessoryView and write the loading code in the same method, the UI will get updated only when all the operations over. A workaround for this is to just set the activityIndicator in the didSelectRowAtIndexPath: method and call the view controller pushing code with some delay.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
[self performSelector:#selector(pushDetailView:) withObject:tableView afterDelay:0.1];
}
- (void)pushDetailView:(UITableView *)tableView {
// Push the detail view here
}
Related
I have the following error. 'indexPath' undeclared (first use in this function).
Code.
didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];
[self performSelector:#selector(pushDetailView:) withObject:tableView afterDelay:0.1];
}
pushDetailView
- (void)pushDetailView:(UITableView *)tableView {
// Push the detail view here
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//load the clicked cell.
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath];
//init the controller.
AlertsDetailsView *controller = nil;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView_iPad" bundle:nil];
} else {
controller = [[AlertsDetailsView alloc] initWithNibName:#"DetailsView" bundle:nil];
}
//set the ID and call JSON in the controller.
[controller setID:[cell getID]];
//show the view.
[self.navigationController pushViewController:controller animated:YES];
}
I think it's because I'm not parsing the indexPath value from didSelectRowAtIndexPath to pushDetailView but I don't know how to approach this.
Could someone please advise?
Thanks.
The problem is that you pushDetailView: method has no indexPath variable on it's scope.
Instead of
- (void)pushDetailView:(UITableView *)tableView {
You should make your method like this:
- (void)pushDetailView:(UITableView *)tableView andIndexPath: (NSIndexPath*) indexPath {
and then indexPath would be declared on the method scope.
Then, inside your didSelectRowAtIndexPath method, replace
[self performSelector:#selector(pushDetailView:) withObject:tableView afterDelay:0.1];
for the code bellow:
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self pushDetailView: tableView andIndexPath: indexPath];
});
This uses GCD to execute the code after a delay, instead of the performSelector: withObject :afterDelay: and here is a nice post explaining why sometimes is better to opt for this aproach
Since you need to provide two arguments and perform after a delay package both arguments in an NSDictionary and pass it:
NSDictionary *arguments = [NSDictionary dictionaryWithObjectsAndKeys:
tableView, #"tableView", indexPath, #"indexPath", nil];
[self performSelector:#selector(pushDetailView:) withObject:arguments afterDelay:0.1];
...
- (void)pushDetailView:(NSDictionary *)arguments {
UITableView *tableView = [arguments objectForKey:#"tableView"];
NSIndexPath *indexPath = [arguments objectForKey:#"indexPath"];
...
Or as #Felipe suggests, use GCD.
i am new to iphone ,my question is in one view i placed a tableview and display some data on that view,i want to add another view(same view to all cell) when select cell(suppose select first cell view will display and newly add view is used to delet that cell information based on cell index)please tell me how to display view.
In the didSelectRowAtIndexPath method in your TableView class; that is where you deal with the selected row. Here is some code that pushes a webViewController onto the screen :
webViewController = [[WebViewController alloc] init];
WebViewController *wvc = [[WebViewController alloc] init];
NSMutableArray *temp = [eventsDict objectForKey:[datesArray objectAtIndex:[indexPath section]]];
EventObject *tempEvent = [temp objectAtIndex:[indexPath row]];
NSString *eventURL = [tempEvent eventLink];
[wvc setEventWebsite:eventURL];
[self setWebViewController:wvc];
[[self navigationController] pushViewController:webViewController animated:YES];
[wvc release];
[webViewController release];
Basically I have an array of Events, it gets the event you touched using [indexPath row], gets the URL within that Event object, passes it to the webViewController, then it pushes the webViewController onto the screen loading the appropriate link. Hope this helps
If you want to display the same view for all cells - simply create one UIView object and change frame in didSelectRowAtIndexPath delegate method. It will something like this -
At first declare infoView in your .h file UIView *infoView, next steps:
- (void)viewDidLoad {
infoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
infoView.hidden = YES;
[tableView addSubview:infoView];
[super viewDidLoad]; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CGPoint st = CGPointMake(tableView.frame.size.width/2, cell.frame.size.height*indexPath.row);
infoView.hidden = NO;
infoView.center = st;}
Don't forget to release infoView in dealloc method
How do make the individual cells go to different View Controller. Right now, using the method im using, if i click the first cell, it takes me to a view controller where i have a pic. now if i go back and click the second cell, it takes me to the same view controller. How do i change this so that all the other individual cells go to individual view controller? what do i add or what do i configure?
here my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
NSUInteger row = [indexPath row];
cell.textLabel.text = [breakdownArray objectAtIndex:row];
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
NSInteger row = [indexPath row];
if (self.bookDetailViewController == nil); {
BookDetailViewController *aBookDetail = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
self.bookDetailViewController = aBookDetail;
[aBookDetail release];
}
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
}
One fairly easy way is in your tableView:didSelectRowAtIndexPath: method. You can check the index path parameter and execute different code paths. Something like:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath {
switch(indexPath.row) {
case 1:
[self.navigationController pushViewController:someViewController animated:YES];
break;
case 2:
[self.navigationController pushViewController:someOtherViewController animated:YES];
break;
}
}
Like most design choices, this is just an example of one possible approach. That's the best I can do in this general case.
I would create a new view controller each time instead of using the same one and just changing its properties, more like the example that you have commented out in tableView:didSelectRowAtIndexPath:. This will only work if you don't access your controller from anywhere other than these methods.
First: Remove bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]]; from your tableView:cellForRowAtIndexPath: method (you should do this anyway, since it is just taking more processor time)
Second: Remove the bookDetailController property from your class. You won't need it anymore.
Third: Implement tableView:didSelectRowAtIndexPath: as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)path {
BookDetailViewController *bookDetailViewController = [[BookDetailViewController alloc] initWithNibName:#"BookDetailView" bundle:nil];
NSInteger row = path.row;
bookDetailViewController.title = [NSString stringWithFormat:#"%#" , [breakdownArray objectAtIndex:row]];
//Any other setup on view controller
Books2010AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.breakdownNavController pushViewController:bookDetailViewController animated:YES];
[bookDetailViewController release];
}
After retrieving search results from a UISearchBar, the results appear in my tableview correctly, but the view is 'greyed out' (see image below)..Any help on this is appreciated, I can't find a solution the Apple documentation.
This is my code that is fired upon hitting the Search button:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
isSearchOn = YES;
canSelectRow = YES;
self.tableView.scrollEnabled = YES;
CityLookup *cityLookup = [[CityLookup alloc] findCity:searchBar.text];
if ([cityLookup.citiesList count] > 0) {
tableCities = cityLookup.citiesList;
}
[cityLookup release];
isSearchOn = NO;
self.searchBar.text=#"";
[self.searchBar setShowsCancelButton:NO animated:YES];
[self.searchBar resignFirstResponder];
[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.tableView reloadData];
}
And this is how the table view is being refreshed:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *kCellID = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *cellValue = [tableCities objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
If you are using the full package with a UISearchDisplayController the following line should remove the search interface:
[self.searchDisplayController setActive:NO animated:YES];
If you are not using UISearchDisplayController I would recommend checking it out and see if it doesn't suit your needs.
Remark: There is nothing in your posted code that would remove whatever view you have used to grey out the table view. So if you are not using UISearchDisplayController look in the code that displays the search interface to see what you need to do to remove it.
Your not hiding your search bar when hitting search, try:
[self.navigationController setNavigationBarHidden:YES animated:YES];
I've got a UITableView with several entries. Upon selecting one, I need it to do a potentially time-consuming network operation. To give the user some feedback, I tried to place a UIActivityIndicatorView in the UITableViewCell. However, the spinner doesn't appear until much later -- after I've done the expensive operation! What am I doing wrong?
- (NSIndexPath *) tableView:(UITableView *) tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner autorelease];
[spinner startAnimating];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryView:activity];
if ([self lengthyNetworkRequest] == nil) {
// ...
return nil;
}
return indexPath;
}
As you can see, I have the spinner being set to the accessoryView before the lengthy network operation. But it only appears after the tableView:willSelectRowAtIndexPath: method finishes.
Once you tell the ActivityIndicator to start animating, you have to give your application's run loop a chance to start the animation before starting the long operation. This can be achieved by moving the expensive code into its own method and calling:
[self performSelector:#selector(longOperation) withObject:nil afterDelay:0];
EDIT: I think you should be using didSelect instead of willSelect.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Try adding [CATransaction flush] right before
if ([self lengthyNetworkRequest] == nil) {
- (NSIndexPath *) tableView:(UITableView *) tableView
willSelectRowAtIndexPath:(NSIndexPath *) indexPath {
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[spinner autorelease];
[spinner startAnimating];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryView:activity];
if ([self lengthyNetworkRequest] == nil) {
//doing the intensive work after a delay so the UI gets updated first
[self performSelector:#selector(methodThatTakesALongTime) withObject:nil afterDelay:0.25];
//you could also choose "performSelectorInBackground"
}
return indexPath;
}
- (void)methodthatTakesALongTime{
//do intensive work here, pass in indexpath if needed to update the spinner again
}