2011-05-08 58 views
0

我是一名初学者,但一直在努力构建这个应用程序。从另一个问题工作,我问:mapkit and table view sample code - iPhone SDK在tableView中使用mapView注释

我能够放在一起(半)功能的tableview和mapview。但是,我不确定如何调用mapview注释来执行numberOfRowsInSection或cellForRowAtIndexPath。

cellForRowAtIndexPath我确定没有正确调用,但我想它是一个combo的numberOfRowsInSection和cellForRowAtIndexPath ..但老实说,我一直在尝试(和失败)3周,阅读我所能我问了这个问题。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

//return [[mapView annotations] count]; 

return 1; } 

所以我试图使用mapview注释的计数,但我甚至不知道这是否工作。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 
    cell =[[[UITableViewCell alloc]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 

}

如果需要任何其他代码,以帮助解释我的情况只是让我知道。

+0

tableView和mapView是否在同一个视图控制器中?如果您在numberOfRowsInSection中取消注释该行,会发生什么情况?它究竟如何失败?是否有编译错误,运行时错误? – Anna 2011-05-08 02:51:57

+0

只能得到1个注释才能进入tableview,而不会在启动时崩溃。我花了几天时间,但我已经解决了这个问题,但numberOfRowsInSection仍然不是很有活力,但我可以忍受它。我会在下面发布我的解决方案。 – jamesC 2011-05-11 17:46:23

回答

0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

//return [[mapView annotations] count]; 

return 20;} 

所以我知道我一次只有20个注释。这仍然不理想,但它的工作原理

-(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;} 

tableview中填充了注释信息。我通过观看Serban Porumbescu的UCDavis课程(他正在讨论桌面视图和对象),找到了我的解决方案。

+0

但是,如果您取消注释numberOfRowsInSection中的那一行,会发生什么错误?您在cellForRowAtIndexPath中的循环是不必要的,且效率低下。每当该行变为可见时,该方法将由表视图调用一次。可以用'cell.textLabel.text = [[[mapView annotations] objectAtIndex:indexPath.row] title];''也可以有内存泄漏(需要做'[注释发布];''return cell;'之前')。 – Anna 2011-05-11 18:16:59

+0

当我替换你的代码时,我得到错误: “***由于未捕获的异常'NSRangeException',原因:'*** - [NSMutableArray objectAtIndex:]:索引0超出空数组的界限''终止应用程序 * **一次调用堆栈:“ – jamesC 2011-05-12 13:51:35

+0

您是否还更改了numberOfRowsInSection以返回实际计数而不是”20“? – Anna 2011-05-12 14:01:14