I'm a beginner but have been working really hard to build this application. Working from another question I asked: mapkit and table view sample code - iPhone SDK
I was able to put together a (semi-)functioning tableview and mapview. However, I'm not really sure how to call the mapview annotations to do numberOfRowsInSection or cellForRowAtIndexPath.
the cellForRowAtIndexPath I'm sure isn't called properly but I'm thinking its a combo of both numberOfRowsInSection and cellForRowAtIndexPath.. But honestly I've been trying (and failing) for 3 weeks and reading all I can before I asked the question.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return [[mapView annotations] count];
return 1; }
So I was trying to use the count of the mapview annotations but I'm not even sure if this is working.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell =[[[UITableViewCell a开发者_如何学Golloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableArray *annotations = [[NSMutableArray alloc] init];
//i'm sure the entire block below is pretty awful but i'm at a standstill
for (MapLocation *annotation in [mapView annotations])
{
[annotations addObject:annotation];
cell.textLabel.text = [[annotations objectAtIndex:indexPath.row]title];
}
return cell; //i'm pretty sure this shouldn't be here
}
If any other code is needed to help explain my situation just let me know.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return [[mapView annotations] count];
return 20;}
So I know that I only have 20 annotations at a time. This is still not ideal but it works
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier]autorelease];}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
}
NSMutableArray *annotations = [[NSMutableArray alloc] init];
if(indexPath.row < [[mapView annotations] count])
{
for (MapLocation *annotation in [mapView annotations])
{
[annotations addObject:annotation];
}
cell.textLabel.text = [[annotations objectAtIndex:indexPath.row]title];
}
return cell;}
The tableview is populated with the annotation information. I found my solution by watching the UCDavis course with Serban Porumbescu (he was discussing tableviews and objects).
精彩评论