Displaying MapView in next VC from Table view cell by taking Latitude and longitude keys from json? - objective-c

I am newbie to ios...please help me in solving this.i need to display map view in next view controller by tapping the row of a table view...
Here is my json:
[{"storeId":"STORE001","name":"Creamstone","latitude":"17.441934745594924","longitude":"78.45833734609187","storeAddress":"Begumpet, OPP: HPS","storeCity":"Hyderabad"},{"storeId":"STORE002","name":"Cream Stone","latitude":"17.37732708258546","longitude":"78.49484514445066","storeAddress":"near malakpet railway station\r\nchaderghat","storeCity":"Hyderabad"}]
here is my LocationTable.m
#import "LocationTable.h"
#import "LocationViewController.h"
#import "Stores.h"
#interface LocationTable ()
{
NSDictionary *coordinates;
}
#end
#define getStores #"http://192.168.28.9:8080/FoodCart/rest/storersrc/storebycity/hyderabad"
#implementation LocationTable
#synthesize json,locations;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Locations";
[self.tableView reloadData];
[self retrieveData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"LocationCell"];
Stores *location = [locations objectAtIndex:indexPath.row];
cell.textLabel.text = location.name;
cell.detailTextLabel.text = location.storeAddress;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
LocationViewController*lvc = [[LocationViewController alloc] init];
Stores *currentlocation = [locations objectAtIndex:indexPath.row];
lvc.latitude= currentlocation.latitude;
lvc.longitude=currentlocation.longitude;
[self.navigationController pushViewController:lvc animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return locations.count;
}
-(void)retrieveData
{
NSError*error;
NSURL * url = [NSURL URLWithString:getStores];
NSData*data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
locations = [[NSMutableArray alloc] init];
for(int i=0;i<json.count;i++)
{
NSString * cStoreId = [[json objectAtIndex:i] objectForKey:#"storeId"];
NSString * cName = [[json objectAtIndex:i] objectForKey:#"name"];
NSNumber * cLatitude = [[json objectAtIndex:i] objectForKey:#"latitude"];
NSNumber * cLongitude = [[json objectAtIndex:i] objectForKey:#"longitude"];
NSString * cStoreAddress = [[json objectAtIndex:i] objectForKey:#"storeAddress"];
Stores *mylocations = [[Stores alloc] initWithstoreId:(NSString *) cStoreId andname:(NSString *) cName andlatitude:(NSNumber *) cLatitude andlongitude:(NSNumber *) cLongitude andstoreAddress:(NSString *) cStoreAddress];
[locations addObject:mylocations];
}
}
#end
Here is my LocationViewController.m:
#import "LocationViewController.h"
#import "MapView.h"
#import "LocationTable.h"
#interface LocationViewController ()
#property (nonatomic,strong) NSNumber *latitude;
#property (nonatomic,strong) NSNumber *longitude;
#property (nonatomic,strong) IBOutlet MKMapView *Mapview;
#end
#implementation LocationViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.Mapview.delegate = self;
MapView *mapPoint = [[MapView alloc] init];
mapPoint.coordinate = CLLocationCoordinate2DMake([self.Stores.latitude doubleValue], [self.Stores.longitude doubleValue]);
// Add it to the map view
[self.Mapview addAnnotation:mapPoint];
// Zoom to a region around the pin
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(mapPoint.coordinate, 500, 500);
[self.Mapview setRegion:region];
self.title = self.Stores.name;
}
Method Declaration Of MKAnnotationView
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
MKPinAnnotationView *view = nil;
static NSString *reuseIdentifier = #"MapView";
view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIdentifier];
if(!view) {
view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
view.canShowCallout = YES;
view.animatesDrop = YES;
}
return view;
}
#end
my jsonactivity is get data from url into table View , but now I want to display that data (latitude and longitude) on map view in the next View Controller, How Could I do this ? please help me, thanks in advance !

If the LocationViewController defined in a storyboard file, then you should create him via the next method:
[[UIStoryboard storyboardWithName:<your_storyboard_name> bundle:nil] instantiateViewControllerWithIdentifier:<your_controller_identifier>];
Otherwise, all IBOutlet-objects will be a nil.

Related

Objective-C use of undeclared identifier 'numberOfSectionsInTableView'

Not sure where I went wrong. My understanding is that importing UIKit will enable use of the numberOfSectionsInTableView method... Any help is appreciated.
//HEADER
#import <UIKit/UIKit.h>
#interface MainScrollingTableVC : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
int selectedIndex;
NSMutableArray *titleArray;
NSArray *subtitleArray;
NSArray *textArray;
}
#end
//IMPLEMENTATION
#import "MainScrollingTableVC.h"
#import "MainExpandingCellTableViewCell.h"
#interface MainScrollingTableVC ()
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation MainScrollingTableVC
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
//index = -1 means no cell is expanded or should expand
selectedIndex = -1;
titleArray = [[NSMutableArray alloc] init];
NSString *string;
//create consecutive array of 8 numbers. 1...2...etc...
for(int ii = 1; ii <= 8; ii++) {
string = [[NSString alloc] initWithFormat:#"Row %i", ii];
[titleArray addObject:string];
}
subtitleArray = [[NSArray alloc] initWithObjects:#"First row", #"Second row", #"Third row", #"Fourth row", #"Fifth row", #"Sixth row", #"Seventh row", #"Eigth row", nil];
textArray = [[NSArray alloc] initWithObjects:#"Apple", #"Orange", #"Grape", #"Banana", #"Kiwi", #"Blueberry", #"Apricot", #"Lemon", nil];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return titleArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"MainExpandingCell";
MainExpandingCell *cell = (MainExpandingCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MainExpandingCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if(selectedIndex == indexPath.row) {
//Do expanded cell stuff
} else {
//Do closed cell stuff
}
cell.titleLabel.text = [titleArray objectAtIndex:indexPath.row];
cell.timeLabel.text = [subtitleArray objectAtIndex: indexPath.row]; //CHECK VARS
cell.textLabel.text = [textArray objectAtIndex:indexPath.row];
int etaLabel = (indexPath.row + 1) * 25;
cell.etaLabel.text = [NSString stringWithFormat:#"%i", eta]
cell.clipsToBounds = YES;
return cell;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You are declaring your methods inside the viewDidLoad method. Objective-C doesn't support method nesting. Fix your whole code with its formatting and add the missing } accordingly.

How to set textlabels of UITableView cells

In my app, I have a map that allows the user to drop pins onto the map. The pins geocode the address and set the address as their title. I made it so that the annotations are stored in an array when they are dropped. I have a tableview, that I want to show the addresses in the cells. I have an array in the tableview class that points to the array in the first view controller, and I somehow need to access the addresses from the array and set them as the textlabels of the cells. I already have the table view display as many cells as there are pins on the map, but I do not know how to access the information I need from the array. Here is some code to help.
Here is the first view controller:
#implementation P2OViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[_worldView setShowsUserLocation:YES];
NSInteger mapTypeValue = [[NSUserDefaults standardUserDefaults]
integerForKey:P2OMapTypePrefKey];
// Update the UI
[mapTypeControl setSelectedSegmentIndex:mapTypeValue];
// Update the map
[self changeMapType:mapTypeControl];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self
action:#selector(press:)];
[longPress setMinimumPressDuration:0.5f]; //user needs to press for 2 seconds
[longPress setDelegate:self];
[_worldView addGestureRecognizer:longPress];
_annotationArray = [[NSMutableArray alloc]init];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];
[_worldView addGestureRecognizer:pan];
number = [NSNumber numberWithInt:0];
}
-(void)press:(UILongPressGestureRecognizer *)recognizer
{
CGPoint touchPoint = [recognizer locationInView:_worldView];
CLLocationCoordinate2D touchMapCoordinate = [_worldView convertPoint:touchPoint toCoordinateFromView:_worldView];
geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
altitude:CLLocationDistanceMax
horizontalAccuracy:kCLLocationAccuracyBest
verticalAccuracy:kCLLocationAccuracyBest
timestamp:[NSDate date]];
[geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(#"reverseGeocoder:completionHandler: called");
if (error) {
NSLog(#"Geocoder failed with error: %#", error);
} else {
CLPlacemark *place = [placemarks objectAtIndex:0];
geocodedAddress = [NSString stringWithFormat:#"%# %#, %# %#", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];
if (UIGestureRecognizerStateBegan == [recognizer state]) {
value = [number intValue];
number = [NSNumber numberWithInt:value + 1];
addressPin = [[MapPoint alloc]initWithAddress:geocodedAddress coordinate:touchMapCoordinate
title:geocodedAddress identifier:number];
NSLog(#"The identifier is %#", number);
[_annotationArray addObject:addressPin];
[_worldView addAnnotation:addressPin];
NSLog(#"The number of pins in the annotation array is: %u",_annotationArray.count);
}
}
}];
}
-(void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
PinViewController *pvc = [[PinViewController alloc]init];
[[self navigationController]pushViewController:pvc
animated:YES];
pvc.label.text = view.annotation.title;
}
- (IBAction)bookmarkPressed:(id)sender
{
P2OTableViewViewController *tvc = [[P2OTableViewViewController alloc]initWithStyle:UITableViewStyleGrouped];
[tvc setValue:_annotationArray];
[[self navigationController]pushViewController:tvc
animated:YES];
}
#end
Here is my TableViewController
#import <UIKit/UIKit.h>
#class P2OViewController;
#interface P2OTableViewViewController : UITableViewController
{
NSMutableArray *_pinArray;
__weak P2OViewController *_pvc;
}
#property (readonly) UILabel *textLabel;
#property (weak,nonatomic) P2OViewController *pvc;
#property (strong,nonatomic) NSMutableArray *pinArray;
-(void)setValue:(NSMutableArray *)value;
#end
#implementation P2OTableViewViewController
#synthesize pinArray = _pinArray, pvc = _pvc;
- (id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
_pinArray = [[NSMutableArray alloc]init];
}
return self;
}
-(void)setValue:(NSMutableArray *)value
{
_pinArray = value;
}
-(id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return _pinArray.count;
}
- (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];
}
return cell;
}
So basically I want the title of the annotations in the array to be the textlabel for the cells. So if there are 5 pins in the array, I want the 5 cells to have the corresponding addresses of the annotations (the title property) as the textlabels. I would appreciate the help.
Before return cell;, you can do cell.textLabel.text = [_pinArray objectAtIndex:indexPath.row] Make sure you're extracting the title property from _pinArray.

UISearchBarDelegate on UITableView with Sections Issue

I'm having issues with implementing the UISearchBarDelegate on my UITableView with sections issue where every time I try to search for something inside the UITableView, either it doesn't display the desired result or it crashes the app with an error saying:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Here's inside my header file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate, UISearchBarDelegate>
{
UITableView *mainTableView;
NSMutableArray *contentsList;
NSMutableArray *searchResults;
NSString *savedSearchTerm;
NSMutableArray *ivSectionKeys;
NSMutableDictionary *ivSectionContents;
}
#property (nonatomic, retain) IBOutlet UITableView *mainTableView;
#property (nonatomic, retain) NSMutableArray *contentsList;
#property (nonatomic, retain) NSMutableArray *searchResults;
#property (nonatomic, copy) NSString *savedSearchTerm;
#property (nonatomic, retain) NSMutableArray *sectionKeys;
#property (nonatomic, retain) NSMutableDictionary *sectionContents;
- (void)handleSearchForTerm:(NSString *)searchTerm;
#end
while this is inside my implementation file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mainTableView;
#synthesize contentsList;
#synthesize searchResults;
#synthesize savedSearchTerm;
#synthesize sectionKeys = ivSectionKeys;
#synthesize sectionContents = ivSectionContents;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *colorKey = #"Colors";
NSString *clothingKey = #"Clothing";
NSString *miscKey = #"Misc";
[contents setObject:[NSArray arrayWithObjects:#"Red", #"Blue", nil] forKey:colorKey];
[contents setObject:[NSArray arrayWithObjects:#"Pants", #"Shirt", #"Socks", nil] forKey:clothingKey];
[contents setObject:[NSArray arrayWithObjects:#"Wankle Rotary Engine", nil] forKey:miscKey];
[keys addObject:clothingKey];
[keys addObject:miscKey];
[keys addObject:colorKey];
self.sectionKeys = keys;
self.sectionContents = contents;
// Restore search term
if (self.savedSearchTerm)
{
self.searchDisplayController.searchBar.text = self.savedSearchTerm;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Save the state of the search UI so that it can be restored if the view is re-created.
self.savedSearchTerm = self.searchDisplayController.searchBar.text;
self.searchResults = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.mainTableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)handleSearchForTerm:(NSString *)searchTerm
{
self.savedSearchTerm = searchTerm;
if (self.searchResults == nil)
{
NSMutableArray *array = [[NSMutableArray alloc] init];
self.searchResults = array;
array = nil;
}
[self.searchResults removeAllObjects];
if ([self.savedSearchTerm length] != 0)
{
for (NSString *currentString in self.sectionContents)
{
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
[self.searchResults addObject:currentString];
}
}
}
}
#pragma mark -
#pragma mark UITableViewDataSource Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger sections = [self.sectionKeys count];
return sections;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
NSString *key = [self.sectionKeys objectAtIndex:section];
return key;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSString *key = [self.sectionKeys objectAtIndex:section];
NSArray *contents = [self.sectionContents objectForKey:key];
NSInteger rows;
if (tableView == [[self searchDisplayController] searchResultsTableView])
rows = [self.searchResults count];
else
rows = [contents count];
NSLog(#"rows is: %d", rows);
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [self.sectionKeys objectAtIndex:indexPath.section];
NSArray *contents = [self.sectionContents objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:indexPath.row];
if (tableView == [self.searchDisplayController searchResultsTableView])
contentForThisRow = [self.searchResults objectAtIndex:indexPath.row];
else
contentForThisRow = [contents objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = contentForThisRow;
return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self handleSearchForTerm:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
self.savedSearchTerm = nil;
[self.mainTableView reloadData];
}
#end
I think my issue is inside my for-loop but I'm not really sure.
You need to update all of your table view data source and delegate methods to handle both tables (you only do this in some):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger sections = [self.sectionKeys count];
return sections;
}
should be:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.tableView) {
NSInteger sections = [self.sectionKeys count];
return sections;
} else { // assume it's the search table
// replace this with the actual result
return numberOfSectionsForSearchTable;
}
}

How to pass data from uitableview to detail view in storyboard

I'm creating my first app in iOS 5 using storyboards. I have a tableviewcontroller embedded in a navigation controller and when you click on the cells in the tableviewcontroller some detail about that cell topic & a URL should be passed to a detail view. I use a PList to populate the tableview. I failed to pass the detail to detail view...
Here is my code (4 classes):
// NormalDataCell.m
#import "NormalDataCell.h"
#implementation NormalDataCell
#synthesize image = _image;
#synthesize nameLabel = _nameLabel;
#synthesize category = _category;
#synthesize webSiteURL = _webSiteURL;
#end
// normalData.m
#import "NormalData.h"
#implementation NormalData
#synthesize name = _name;
#synthesize image = _image;
#synthesize webSiteURL = _webSiteURL;
#synthesize category = _category;
- (id)initWithItem:(NSDictionary *)item {
self = [super init];
if (self) {
_name = [item objectForKey:#"name"];
_image = [UIImage imageNamed:[item objectForKey:#"image"]];
_webSiteURL = [item objectForKey:#"webSiteURL"];
_category = [item objectForKey:#"category"];
}
return self;
}
#end
#implementation NormalCategory
#synthesize category = _category;
#synthesize normalList = _normalList;
- (id)initWithArray:(NSMutableArray *)normalList {
self = [super init];
if (self) {
NSMutableArray *list = [[NSMutableArray alloc]init];
for (NSDictionary *item in normalList) {
NormalData *data = [[NormalData alloc]initWithItem:item];
[list addObject:data];
}
NormalData *data = [list objectAtIndex:0];
_category = data.category;
_normalList = list;
}
return self;
}
#end
// normalViewController.m
#import "NormalViewController.h"
#import "NormalData.h"
#import "NormalDataCell.h"
#import "NormalDetailView.h"
#interface NormalViewController ()
{
#private
NSMutableArray *_cellContentList;
}
#end
#implementation NormalViewController
#synthesize tableView; // Add this line of code
#synthesize roleArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://vfttx.web.fc2.com/normal.plist"];
NSMutableArray *array = [NSMutableArray arrayWithContentsOfURL:url];
_cellContentList = [[NSMutableArray alloc]init];
for (NSMutableArray *item in array)
{
NormalCategory *category = [[NormalCategory alloc]initWithArray:item];
[_cellContentList addObject:category];
}
}
//How do I pass the name & webSiteURL to DetailView?
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"TableToWebSegue"]) {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSString *cellName = [[_cellContentList objectAtIndex:path.row] objectForKey:#"name"];
[segue.destinationViewController setDetailItem2:cellName];
NSString *urlPath = [[_cellContentList objectAtIndex:path.row] objectForKey:#"webSiteURL"];
[segue.destinationViewController setWebSiteURL:urlPath];
}
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_cellContentList count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NormalCategory *category = [_cellContentList objectAtIndex:section];
return [category category];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NormalCategory *category = [_cellContentList objectAtIndex:section];
return [category.normalList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"normalDataCell";
NormalDataCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[NormalDataCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NormalCategory *category = [_cellContentList objectAtIndex:[indexPath section]];
NormalData *data = [category.normalList objectAtIndex:[indexPath row]];
[cell.nameLabel setText:data.name];
[cell.image setImage:data.image];
cell.webSiteURL = data.webSiteURL;
return cell;
}
#end
// NormalDetailView.m
#import "NormalDetailView.h"
#interface NormalDetailView ()
#end
#implementation NormalDetailView
#synthesize webSiteURL;
#synthesize webView;
#synthesize name = _name;
#synthesize detailItem2;
#synthesize testNameLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
testNameLabel.text = detailItem2;
//If the URL is right, it will work.
NSString *strUrl = webSiteURL;
strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:strUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
#end
Presumably you're using segues to move between the different views.
Implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender on the view controller containing your table view and it will be called after the user taps the cell. It contains a reference to the segue object which you can use to find the sourceViewController and the destinationViewController, you can use these to set the data in the destination.

Simple twitter program not working

I'm using this tutorial to practice creating an extremely basic twitter app: http://www.codeproject.com/Articles/312325/Making-a-simple-Twitter-app-using-iOS-5-Xcode-4-2#setting-up-the-table-view
The only difference in my app is I'm only using tableView ViewController. I can't seem to get this to work.
ViewController.h
#interface ViewController : UIViewController {
NSArray *tweets;
}
-(void)fetchTweets;
#property (retain, nonatomic) IBOutlet UITableView *tableView;
#end
ViewController.m
#import "ViewController.h"
#import "Twitter/Twitter.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize tableView = _tableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self fetchTweets];
}
- (void)fetchTweets
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL:
[NSURL URLWithString: #"https://api.twitter.com/1/statuses/public_timeline.json"]];
NSError* error;
tweets = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return tweets.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TweetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *tweet = [tweets objectAtIndex:indexPath.row];
NSString *text = [tweet objectForKey:#"text"];
NSString *name = [[tweet objectForKey:#"user"] objectForKey:#"name"];
cell.textLabel.text = text;
cell.detailTextLabel.text = [NSString stringWithFormat:#"by %#", name];
return cell;
}
- (void)viewDidUnload
{
_tableView = nil;
[self setTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
you forgot to define delegate & datasource for your table, and didn't implement the protocols right as far as i see in your code,
try in your .h file:
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
// your implementation...
}
and in your .m file in viewDidLoad
self.tableView.dataSource = self;
self.tableView.delegate = self;
the numberOfRows, cellForRow, etc... methods wont work until you define your delegate & datasource for this table :)