Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-08-2010, 04:39 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 24
dmelgoza is on a distinguished road
Question tableView problem

I have a tableview that is acting wrong. Please help.

I have a plist with a list of buildings on my campus, I have a tableview loading the plist and then dropping a pin on the location of that building in my mkmapview. Everything worked fine...until.

I decided to add sections and sectionIndexTitlesForTableView .

I added a numberOfRowsInSection to tell the tableView how many items would be in each section and I then made the first cell of each section to start off at a certain point in the list (from the plist).

Everything looks fine, until I click on the actual cells. If I click on the first cell of every section it will drop the pin that belongs to the fist location on my plist. The second item of each section drops the pin belonging to the second item in the plist, so on and so forth. Please help if you can!

PS: I started off with the worldcities sample and hacked the crap out of it, but I'm stuck here.

Code:
//MapViewController.h***********************************************
#import <MapKit/MapKit.h>

#import "WorldCitiesListController.h"

@interface MapViewController : UIViewController <MKMapViewDelegate, WorldCitiesListControllerDelegate>
{
    MKMapView *mapView;
    WorldCitiesListController *worldCitiesListController;
    UINavigationController *worldCitiesListNavigationController;
}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;
@property (nonatomic, retain, readonly) WorldCitiesListController *worldCitiesListController;
@property (nonatomic, retain, readonly) UINavigationController *worldCitiesListNavigationController;

- (IBAction)viewPlacesList;
- (IBAction)setMapType:(id)sender;

@end
Code:
//MapViewController.m***********************************************
#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import "WorldCity.h"
#import "MyAnnotation.h"

@implementation MapViewController

@synthesize mapView;


- (WorldCitiesListController *)worldCitiesListController
{
    if (worldCitiesListController == nil)
    {
        worldCitiesListController = [[WorldCitiesListController alloc] initWithStyle:UITableViewStylePlain];
        worldCitiesListController.delegate = self;
        worldCitiesListController.title = @"Choose a Building:";
    }
    return worldCitiesListController;
}

- (UINavigationController *)worldCitiesListNavigationController
{
    if (worldCitiesListNavigationController == nil)
    {
        worldCitiesListNavigationController = [[UINavigationController alloc] initWithRootViewController:self.worldCitiesListController];
    }
    return worldCitiesListNavigationController;
}

- (void)viewDidUnload
{
	self.mapView = nil;
}

- (void)dealloc
{
    [mapView release];
    [worldCitiesListController release];
    [worldCitiesListNavigationController release];

    [super dealloc];
}

- (IBAction)viewPlacesList
{
    [self.navigationController presentModalViewController:self.worldCitiesListNavigationController animated:YES];
}

- (IBAction)setMapType:(id)sender
{
    switch (((UISegmentedControl *)sender).selectedSegmentIndex)
    {
        case 0:
        {
            mapView.mapType = MKMapTypeHybrid;
            break;
        } 
        case 1:
        {
            mapView.mapType = MKMapTypeSatellite;
            break;
        } 
        default:
        {
            mapView.mapType = MKMapTypeStandard;
            break;
        } 
    }
}

- (void)animateToWorld:(WorldCity *)worldCity
{    
    MKCoordinateRegion current = mapView.region;
    MKCoordinateRegion zoomOut = { { (current.center.latitude + worldCity.coordinate.latitude)/2.0 , (current.center.longitude + worldCity.coordinate.longitude)/2.0 }, {.005, .005} };
    [mapView setRegion:zoomOut animated:YES];
}

- (void)animateToPlace:(WorldCity *)worldCity
{
    MKCoordinateRegion region;
    region.center = worldCity.coordinate;
    MKCoordinateSpan span = {0.0009, 0.0009};
    region.span = span;
    [mapView setRegion:region animated:YES];
}

- (void)worldCitiesListController:(WorldCitiesListController *)controller didChooseWorldCity:(WorldCity *)aPlace
{
    [self.navigationController dismissModalViewControllerAnimated:YES];
    self.title = aPlace.name;
    MKCoordinateRegion current = mapView.region;
    if (current.span.latitudeDelta < 10)
    {
        [self performSelector:@selector(animateToWorld:) withObject:aPlace afterDelay:0.3];
        [self performSelector:@selector(dropPin:) withObject:aPlace afterDelay:1.7]; 
		
    }
    else
    {
        [self performSelector:@selector(animateToPlace:) withObject:aPlace afterDelay:0.3];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];

	mapView.mapType = MKMapTypeHybrid; 
	
	//te encuentra
	mapView.showsUserLocation = YES;
	
	
	
	MKCoordinateRegion region;
    region.center.latitude = 29.446464;
    region.center.longitude = -98.496680;
    region.span.latitudeDelta = 0.005;
    region.span.longitudeDelta = 0.005;
	[self.mapView setRegion:region animated:YES];
	
	[self.view addSubview:mapView];
	
	
	[mapView setDelegate:self];
}

- (void)dropPin:(WorldCity *)worldCity
{
	//el Moody
	MKCoordinateRegion Moody;
	Moody.center = worldCity.coordinate;
	
	//el Moody
	MyAnnotation *pin2 = [[MyAnnotation alloc] init];
	pin2.title = worldCity.name;
	pin2.subtitle = @"";
	pin2.coordinate = Moody.center;
	[mapView addAnnotation:pin2];
	[pin2 release];
	
	


}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id )annotation
{
    MKPinAnnotationView *pinView = nil;
    if(annotation != mapView.userLocation)
    {
		static NSString *defaultPinID = @"com.invasivecode.pin";
		pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
		if ( pinView == nil )
            pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
        pinView.pinColor = MKPinAnnotationColorRed;
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
    }
    else
        [mapView.userLocation setTitle:@"I am here"];
    return pinView;
}
//----------------------------

@end
dmelgoza is offline   Reply With Quote
Old 04-08-2010, 04:39 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 24
dmelgoza is on a distinguished road
Default here are some other pages, that may apply

Code:
//WorldCitiesListController.h***********************************************
#import <UIKit/UIKit.h>
#import "WorldCity.h"

@protocol WorldCitiesListControllerDelegate;

@interface WorldCitiesListController : UITableViewController <NSFetchedResultsControllerDelegate>
{
	NSMutableArray* cityList;
    
    id <WorldCitiesListControllerDelegate> delegate;
    BOOL addingNewPlace;
}

@property (nonatomic, retain) NSMutableArray* cityList;
@property (nonatomic, assign) id <WorldCitiesListControllerDelegate> delegate;

@end

@protocol WorldCitiesListControllerDelegate

- (void)worldCitiesListController:(WorldCitiesListController *)controller didChooseWorldCity:(WorldCity *)aPlace;

@end




Code:
//WorldCitiesListController.m***********************************************


#import "WorldCitiesListController.h"
#import "WorldCity.h"

@implementation WorldCitiesListController

@synthesize cityList, delegate;

//sections added to buildings
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 15;
}

//title the sections
- (NSString *)tableView:(UITableView *)tableView 
titleForHeaderInSection:(NSInteger)section
{
	if ( section == 0) return @"A";
	if ( section == 1) return @"B";
	if ( section == 2) return @"C";
	if ( section == 3) return @"E";
	if ( section == 4) return @"F";
	if ( section == 5) return @"G";
	if ( section == 6) return @"K";
	if ( section == 7) return @"L";
	if ( section == 8) return @"M";
	if ( section == 9) return @"N";
	if ( section == 10) return @"O";
	if ( section == 11) return @"P";
	if ( section == 12) return @"S";
	if ( section == 13) return @"U";
	if ( section == 14) return @"V";
	return @"Other";
}



//Added side letters to tableview
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
	NSArray *sectionTitles = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"E",@"F",@"G",@"K",@"L",@"M",@"N",@"O",@"P",@"S",@"U",@"V",nil];
	return sectionTitles;
	
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // load our data from a plist file inside our app bundle
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cityList" ofType:@"plist"];
    NSArray *cities = [NSArray arrayWithContentsOfFile:path];
    
    self.cityList = [[[NSMutableArray alloc] initWithCapacity:[cities count]] autorelease];

    NSDictionary *city;
    for (city in cities)
    {
        WorldCity *newCity = [[WorldCity alloc] init];
        newCity.name = [city objectForKey:@"cityNameKey"];
        newCity.latitude = [city objectForKey:@"latitudeKey"];;
        newCity.longitude = [city objectForKey:@"longitudeKey"];
        
        [self.cityList addObject:newCity];
        [newCity release];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}


#pragma mark -
#pragma mark Table view methods

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if ( section == 0) return 2;
	if ( section == 1) return 1;
	if ( section == 2) return 9;
	if ( section == 3) return 2;
	if ( section == 4) return 1;
	if ( section == 5) return 2;
	if ( section == 6) return 1;
	if ( section == 7) return 4;
	if ( section == 8) return 3;
	if ( section == 9) return 2;
	if ( section == 10) return 2;
	if ( section == 11) return 1;
	if ( section == 12) return 1;
	if ( section == 13) return 1;
	if ( section == 14) return 1;
	return 0;
	return [self.cityList count];
    
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const kPlacesCellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kPlacesCellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kPlacesCellIdentifier] autorelease];
        cell.editingAccessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    }

//I'm making each section start off at a diff place of the list here.
	int theRow = indexPath.row;
	if ( indexPath.section ==1 ) theRow += 2;
	if ( indexPath.section ==2 ) theRow += 3;
	if ( indexPath.section ==3 ) theRow += 12;
	if ( indexPath.section ==4 ) theRow += 14;
	if ( indexPath.section ==5 ) theRow += 15;
	if ( indexPath.section ==6 ) theRow += 17;
	if ( indexPath.section ==7 ) theRow += 18;
	if ( indexPath.section ==8 ) theRow += 22;
	if ( indexPath.section ==9 ) theRow += 25;
	if ( indexPath.section ==10 ) theRow += 27;
	if ( indexPath.section ==11 ) theRow += 29;
	if ( indexPath.section ==12 ) theRow += 30;
	if ( indexPath.section ==13 ) theRow += 31;
	if ( indexPath.section ==14 ) theRow += 32;
    
    WorldCity *worldCity = [self.cityList objectAtIndex:theRow];
    cell.textLabel.text = worldCity.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%.3f, %.3f", worldCity.coordinate.latitude, worldCity.coordinate.longitude];
	
	
	
	

	
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [delegate worldCitiesListController:self didChooseWorldCity:[self.cityList objectAtIndex:indexPath.row]];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
	[cityList release];
    
    [super dealloc];
}

@end
dmelgoza is offline   Reply With Quote
Reply

Bookmarks

Tags
mkmapview, table, table cell, table view

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 315
14 members and 301 guests
chemistry, Domele, Fstuff, givensur, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, kapps11, newDev, SLIC, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,874
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:25 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0