2012-08-16 100 views
1

我想填充我的表格視圖使用我的批註數組,但XCode似乎給我任何時候添加此代碼的斷點。註解和表格視圖

if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

NSMutableArray *annotations = [[NSMutableArray alloc] init]; 

if(indexPath.section == 0) 
{ 
    for(Location *annotation in [(MKMapView *)self annotations]) 
    { 
     if(![annotation isKindOfClass:[MKUserLocation class]]) 
     { 
    } 
    } 
    cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title]; 
} 
return cell; 

我的註釋:

CLLocationCoordinate2D thecoordinate59; 

thecoordinate59.latitude = 51.520504; 
thecoordinate59.longitude = -0.106725; 
Location *ann1 = [[Location alloc] init]; 
ann1.title [email protected]"Antwerp"; 
ann1.coordinate = thecoordinate1; 

NSMutableArray *annotations = [NSMutableArray arraywithObjects: ann.. ann59, nil]; 

[map addAnnotations:annotations]; 
+0

在此處發佈整個'tableView:cellForRowAtIndexPath:'方法。這有點凌亂。 – robertvojta 2012-08-16 12:14:29

+0

什麼是錯誤? – Dustin 2012-08-16 12:23:57

+0

for(在[(MKMapview *)自我註釋中的位置*註釋 – 2012-08-16 12:28:20

回答

0

您彙報該行

,我看到的問題崩潰(我認爲)是崩潰的原因是

cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title]; 

annotations數組剛剛在本地初始化了幾個不含任何值的語句。

+0

我有註釋的與聲明的NSMutableArray *註釋在我的詳細視圖的陣列。 – 2012-08-16 13:02:56

+0

所以你怎麼能這麼說,你的局部變量'annotations'在代碼中沒有被考慮在這裏而不是你提到的那個? – samfisher 2012-08-16 13:05:18

+0

我無法在表格視圖中填充地圖註釋。 – 2012-08-16 13:18:09

2

cellForRowAtIndexPath,您聲明命名annotations一個新的,局部變量,無關與annotations陣列要創建在viewDidLoad(我認爲這就是你要添加的註釋)。

然後在cellForRowAtIndexPath,這條線:

for(Location *annotation in [(MKMapView *)self annotations]) 

失敗,因爲在self沒有annotations財產。在viewDidLoad中,您聲明瞭一個名爲annotations的局部變量,但它在該方法之外是不可見的或可訪問的。

上述行的另一個問題是,您將self作爲MKMapView *投射。最有可能的selfUIViewController。它包含地圖視圖,但本身不是一個。


您需要首先在詳細視圖的類級別聲明annotations數組,以便在所有方法中都可用。在詳細信息視圖.h文件中:

@property (nonatomic, retain) NSMutableArray *annotations; 

順便說一句,所以它不與地圖視圖自己的annotations財產弄得我命名爲不同的東西。

在.M,合成它:

@synthesize annotations; 

viewDidLoad,創造這樣的:

self.annotations = [NSMutableArray arraywithObjects... 
[map addAnnotations:self.annotations]; 

numberOfRowsInSection方法,返回數組的計數:

return self.annotations.count; 

然後在cellForRowAtIndexPath

if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

if(indexPath.section == 0) 
{ 
    cell.textLabel.text = [[self.annotations objectAtIndex:indexPath.row] title]; 
} 
return cell; 
+0

是在同一個視圖控制器的地圖和表格?在另一評論中你提到了一個細節視圖。什麼是「masterviewcontroller」,什麼是「詳細視圖」? – Anna 2012-08-16 14:34:34

+0

我收到 住宅「註釋」錯誤不是類型「MasterViewController *」 任何時候我用 – 2012-08-16 14:39:37

+0

我使用SPLITVIEW爲iPad self.annotations的對象中找到。表視圖的masterviewcontroller和的DetailView的地圖 – 2012-08-16 14:40:38