- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
BlogRssParser *blogRss = [[BlogRssParser alloc] init];
[blogRss setSelectedObject:selectedObject];
RssFunViewController *rssFun = [[RssFunViewController alloc] initWithNibName:#"RssFunViewController" bundle:nil];
[self.navigationController pushViewController:rssFun animated:YES];
[rssFun release];
}
This is my code so when the user taps the row, its supposed to take the value from the row and insert it into here:
NSString *terms = [[[self selectedObject] valueForKey:#"data"]description];
NSLog(#"%#", terms);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://news.search.yahoo.com/rss?ei=UTF-8&p=%#&fr=news-us-ss", terms]];
But "terms" is showing up as (null) in the log? Not the value of the clicked row
I see, that u used some codes, I posted here for you. But you shouldnt just copy and paste it.
In my code selectedObject is not a member, as it is absolutely not necessary.
This line
NSManagedObject *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
points to a non-member variable
while
[self selectedObject]
points to a member — a totally different thing.
edit
Is the second part in the TableViewContoller or in the DetailedView?
Related
I have a requirement that my UITableview should load data after retrieving data from web service. When retrieving huge data, my uitableview is displaying empty. Instead of that, a UIActivityIndicator should appear until data is loaded into the UITableview.
I am trying as below, but not able to get the desired functionality. Please help.
Also let me suggest what is the best approach to load data into UITableview, when data returned from webservice is huge.
Thanks in advance.
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
UILabel *lblName = (UILabel *)[cell viewWithTag:1];
[lblName setText:[myArray objectAtIndex:[indexPath row]]];
return cell;
}
- (void)dataLoading
{
myActivityIndicator=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[myActivityIndicator setFrame:CGRectMake(140, 170, 40, 40)];
[self.view addSubview:myActivityIndicator];
[myActivityIndicator startAnimating];
NSString *playersDatalink = [NSString stringWithFormat:#"Webservice link"];
NSURL *JsonPlayersDataURL = [NSURL URLWithString:playersDatalink];
NSString *JsonPlayersData = [[NSString alloc] initWithContentsOfURL:JsonPlayersDataURL];
SBJSON *playersDataParser = [[SBJSON alloc] init];
NSDictionary *PlayersDicdata = (NSDictionary *) [playersDataParser objectWithString:JsonPlayersData error:nil];
NSDictionary *playersdata = (NSDictionary *) [PlayersDicdata objectForKey:#"players"];
NSLog(#"palyersdata is =%#",playersdata);
self.myArray = [NSMutableArray arrayWithCapacity:[playersdata count]];
for (NSDictionary *details in playersdata) {
[self.myArray addObject:[details objectForKey:#"teamid"]];
}
if ([playersdata count]==0) {
UIAlertView *myalert = [[UIAlertView alloc]initWithTitle:#"Webserive error " message:#"No data returned from webservice" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[myalert show];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(dataLoading) withObject:nil afterDelay:0.0];
[myActivityIndicator stopAnimating];
}
hi Download This zip http://www.sendspace.com/file/l48jxg Unzipe this file you getting two .h and .m for loading Indicatore view,. drag and drop this into Your Project.
Now you can use this like Bellow:-
if you wish to show Activity Indicator till you data not Load:-
import LoadingView.h into your Class,
create Object
loadingView =[LoadingView loadingViewInView:self.view:#"Please Wait.."];
at this time put this code of like olso yourTableView.userInterfaceEnable=FALSE;
and at the data Load finish method put this
[loadingView removeFromSuperview];
yourTableView.userInterfaceEnable=TRUE;
you getting loading View like:-
UPDATE
It doesn't metter your TableView delegate executing first.
1) If you Using NSURLConnection For Parsiong WebServices you can Reload Yout TableView Data after loading Finish at:-
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//your code
[yourTableView reloadData];
}
2) if you using NSXMLParser For Parsiong WebServices you can Reload Yout TableView Data after loading Finish at:-
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//your code
[yourTableView reloadData];
}
Try this Activity Indicator which stops the user interaction until your data is loaded.
use MBProgressHud available on github
Have a Happy Coding..
I have a model that follows this:
Then I want to stock a city from a UITableView. The problem is, that I want to hold them in the iPhone (Database) until it reach 10, after that it erases the first and adds the last. I have the idea, but I can't just reach to add anything and i'm confused.
When I click a city, it pushes to the next view sending from a response JSON the id and the name of the city.. Thats ok. But I want to store those in order to make the same call but from another UITableView (under it) So it's like the "Last cities searched" something lik that.
I want to save the id,name and after that load it in the other tableView showing only the name. With that i'm Ok, but I can't reach to make the stock happend.
Code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailTaxi *detailView = [[DetailTaxi alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
NSString *nomville = [[_jsonDict objectAtIndex:indexPath.row] objectForKey:#"label"];
NSString *idVille = [[_jsonDict objectAtIndex:indexPath.row] objectForKey:#"id"];
detailView.title = nomville;
detailView.idVille = idVille;
NSLog(#"Valor: %#", nomville);
NSLog(#"Valor: %#", idVille);
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSString *insert = [[NSString alloc] initWithFormat:#"%#,%#",idVille,nomville];
NSManagedObject *newVille;
NSArray *insertVilles = [insert componentsSeparatedByString:#","];
for(NSString *insert in insertVilles) {
newVille = [NSEntityDescription insertNewObjectForEntityForName:#"Ville" inManagedObjectContext:context];
[newVille setValue:[insertVilles objectAtIndex:0] forKey:#"id"];
[newVille setValue:[insertVilles objectAtIndex:1] forKey:#"nom"];
}
[self.navigationController pushViewController:detailView animated:YES];
}
Just add a new attribute of type NSDate which you set to the current date upon creating a new entry. Once you retrieve the cities again, use a sort descriptor to sort the cities by the time they were saved and use a fetch limit.
If deleting the previous cities is necessary, the date attribute will prove useful as well.
I don't understand why the code isn't working, and nothing I found seems to be working. So I've come here for help. Ultimately, I want to be able to send my link to Safari whenever I click on the "View" button, and I want the link to be copied whenever I click on the "Copy" button.
Here's the code (under " - (void)viewDidLoad "):
NSMutableArray *sites = [[NSMutableArray alloc] initWithObjects:#"http://www.apple.com/", #"http://www.youtube.com/", #"http://maps.google.com/", #"http://ww.google.com/", #"http://www.stackoverflow.com/", nil];
self.cloudsendList = sites;
Here is the code (under " - (IBAction) touchUpInsideAction:(UIButton*)button "):
NSIndexPath* indexPath = [tableView indexPathForCell:sideSwipeCell];
NSUInteger index = [buttons indexOfObject:button];
NSDictionary* buttonInfo = [buttonData objectAtIndex:index];
if ([[buttonInfo objectForKey:#"title"] isEqualToString:#"View"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: #"%#", indexPath.row]]];
} else if ([[buttonInfo objectForKey:#"title"]isEqualToString:#"Copy"]) {
NSString *message = indexPath.row;
[UIPasteboard generalPasteboard].string = message;
Just to note, I am able to see the NSMutableArray data on each cell, I just can't grab it. I've also tried to insert "cloudsendList indexPath.row" instead of just "indexPath.row", but it didn't work. I hope this gives you enough information, and any bit of help would be really appreciated. Also, I apologize if I sound kind of noobish; I'm still sort of new to Objective-C programming. Thanks! :)
indexPath.row is an NSInteger, not the text at that location. This means your NSString *message is getting the integer value of the row you are on (0, 1, 2...). Try using that location as an index when pulling from your sites/cloudsendList array.
Ex.
NSString *message = [cloudsendList objectAtIndex:indexPath.row];
UPDATE:
To open the browser, use the same concept.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat: #"%#", [cloudsendList objectAtIndex:indexPath.row]]]];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1 && indexPath.row == 0) {
NSString *requestString = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/xml?origin=%#&destination=%#,OK&sensor=false",startField.text,endField.text];
NSURL *url = [[NSURL alloc] initWithString:requestString];
NSData *tempData =[[NSData alloc] initWithContentsOfURL:url];
NSString *Str = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
NSLog(#"%#",Str);
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
xml1 *parser = [[xml1 alloc] init];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success){
NSLog(#"No Errors");
arySteps=[[NSMutableArray alloc]init];
arySteps=[parser.ListofSteps1 mutableCopy];
}
else
NSLog(#"Error Error Error!!!");
controller.arayStep=[[NSMutableArray alloc]init];
controller.arayStep=[parser.ListofSteps1 copy];
controller= [[TableView alloc] initWithNibName:#"TableView" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
In above code i have parsed XML data. In XML-parser file i have store objects in array that is ListofSteps1. Now i access in root-view controller class which is shown in above code. So in root-view class ListofSteps1 assign value to array of root-view controller. Now i want to assign value of array of root-view controller to next view's array which is also maintain in above code but array of next view is not getting value. What is problem in this code so it not getting value?
There are a couple of things wrong with your code above. You alloc and init a fresh array only to write over it on two separate occasions in your method implementation:
arySteps=[[NSMutableArray alloc]init]; // This line serves absolutely no purpose
arySteps=[parser.ListofSteps1 mutableCopy]; // This line is sufficient on its own
Calling accessor methods on an null pointer will do you absolutely no good. You need to instantiate the object before you attempt to set its properties:
controller= [[TableView alloc] initWithNibName:#"TableView" bundle:nil];
controller.arayStep=[parser.ListofSteps1 copy];
These mistakes are very basic and indicate that you need to work on the fundamentals before you can get too much further. I would suggest that you pick up a good book on Objective-C - for example, Programming in Objective-C by Stephen Kochan - and read it thoroughly. A solid understanding of the core concepts will serve you well going forward.
i got an NSArray which gets filled in the init Method of my UITableViewController.
i use this object in "didSelectRowAtIndexPath" for pushing another tableviewcontroller.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:singleCatTableViewController animated:YES];
[singleCatTableViewController release];
}
this works a few times when i start my application. after selecting a row and getting back to the main uitableview controller at a rondom point my application crashes after selecting a row.
with some nslogs i found out, that it crashes if i try to use my "categories" object.
so
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"before");
NSLog(#"cats: %#", categories);
NSLog(#"after");
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:singleCatTableViewController animated:YES];
[singleCatTableViewController release];
}
with that code my application crashes after "before" ... "after" never shows up.
i dont know why my "categories" object is crashing my application ?!
my categories object is defined in my header file and has a #property (nonatomic, retain). i synthesize it and releasing it in my dealloc method.
anyone has an idea?
// edit:
some more details here, because of the comments:
Debugger Console says: "Program received signal: “EXC_BAD_ACCESS”.
i create the category array like this:
- (void)initCategories {
NSString *path = [[NSBundle mainBundle] pathForResource:#"Categories" ofType:#"plist"];
[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]];
}
calling this method in my initwithstyle method
[self initCategories];
my other custom initializing method looks something like this:
- (id)initWithStyle:(UITableViewStyle)style category:(NSDictionary*)cat {
if (self = [super initWithStyle:style]) {
currentCategory = cat;
items = [[NSMutableArray alloc] init];
self.title = [currentCategory objectForKey:#"TITLE"];
//XLog("%#", currentCategory);
}
return self;
}
ok, first thing is ;
[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]];
you have a leak here. just use
categories = [[NSArray alloc] initWithContentsOfFile:path];
Crash occurs in here;
currentCategory = cat;
you have to retain, use;
currentCategory = [cat retain];
These are the problems I see in posted code, if you have not any mistake in the rest of the program, it should be fine with these fixes.
If you are creating your array something like this:
NSArray *tmpArray = [[NSArray alloc] initWithBlah ...];
Make sure that you assign it using the synthesized getter by using this code:
self.categories = tmpArray;
[tmpArray release];
If you do:
categories = tmpArray;
[tmpArray release];
the instance variable will not be retained at all.