2012-06-28 157 views
1

我想動態添加行。我有建築物名稱的tableview列表。如果有人選擇建築物(didSelectRowAtIndexPath),那麼建築物的各個樓層應該作爲子區域動態添加。它像最大化和最小化各個建築物列表選擇的子範圍。我該怎麼做呢。在此先感謝...通過在tableview iPhone錯誤中選擇tableview行來添加動態子行?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
// There is only one section. 
if (tableView == indoortable || tableView == indoortable_iPad) 
{ 
    return 1; 
} 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of time zone names. 
if (tableView == indoortable || tableView == indoortable_iPad) 
{ 
    return [indoorZones count]; 

} 

} 

的cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

if (tableView == indoortable || tableView == indoortable_iPad) 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

     cell.selectionStyle = UITableViewCellSelectionStyleGray;    //cell bg 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    }  
    // Set up the cell. 
    //cell.textLabel.text = [copyListOfItems objectAtIndex:indexPath.row]; 

    cell.textLabel.text =[indoorZones objectAtIndex: indexPath.row]; 
    //[cell setIndentationLevel:[[self.indoorZones objectAtIndex:indexPath.row] intValue]]; 
    return cell; 
} 
} 

didSlectRowAtIndexPath方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
zonesFloor = [[NSMutableArray alloc]init]; 
zonesFloorA = [[NSArray alloc] initWithObjects:@"Gr fl",@"1st fl",@"2nd fl",nil]; 

[zonesFloor addObject:zonesFloorA]; 
if (tableView == indoortable) 
{ 

    NSUInteger i=indexPath.row+1; 
    for (NSArray *count in self.indoorZones) //app is crashing here giving error.......Collection <__NSArrayM: 0x4b1d550> was mutated while being enumerated. 
{ 

     [zonesFloor addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
     [self.indoorZones insertObject:zonesFloor atIndex:i++]; 


    } 

    [[self indoortable] beginUpdates]; 
    [[self indoortable] insertRowsAtIndexPaths:(NSArray *)zonesFloor withRowAnimation:UITableViewRowAnimationNone]; 
    [[self indoortable] endUpdates]; 

    } 


if (tableView == indoortable_iPad) 
{ 

    //some logic 
} 

[tableView deselectRowAtIndexPath:selectedIndexPath animated:NO]; 
} 

它提供了以下錯誤[__NSArrayI比較:]:或者[NSIndexPath _fastCStringContents:]:無法識別的選擇器發送給實例。我嘗試了很多方法,但可能是我缺乏某個地方。請建議。提前致謝。

回答

0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //NSIndexPath *selectedIndexPath = [self.indoortable indexPathForSelectedRow]; 
zonesFloorA = [[NSArray alloc] initWithObjects:@"Gr fl",@"1st fl",@"2nd fl",nil]; 

if (tableView == indoortable) 
{ 
    for (NSString *str in zonesFloorA) { 
     [indoorZones addObject:str]; 
    } 
    //[[self indoortable] beginUpdates]; 
    //[[self indoortable] insertRowsAtIndexPaths:(NSArray *)zonesFloor withRowAnimation:UITableViewRowAnimationNone]; 
    //[[self indoortable] endUpdates]; 
} 
if (tableView == indoortable_iPad) 
{ 

//some logic 
} 

    [tableView reloadData]; 

} 

可能這個滿足您的要求

+0

它給了我錯誤[__NSArrayI addObject:]:無法識別的選擇器發送到實例... –

+0

讓你的數組indoorZones NSMutableArray我認爲它是nsarray .... – Abhishek

+0

使它變成indoorZones Mutablearray現在它不顯示在tableview列表中 –

0

好了,所以不要太刻薄,但這裏有幾乎太多的問題計數。

讓我們先從如何的tableView的工作,這樣你就可以開始解決這個問題一個基本的解釋:

首先,的tableView問有多少部分是在表通過調用:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

} 

在你的代碼中,你只需告訴它它有一個部分。但是,在以後的代碼中,當您嘗試向表中添加行時,您告訴它您想要將行添加到第二部分(索引爲1)。因此,您需要將這些行添加到第0部分,或者更新上述方法以告訴它,有時會有兩個部分。

二,問的tableView有多少行表中的每個部分通過調用:

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

} 

在代碼中,你只是在返回區的數量。但是,如上所述,您需要包含已添加到表中的行。如果要將它們添加到不同的部分,則需要返回不同的值,具體取決於section變量中要求的索引部分中的行數。如果它們都在同一部分中,則需要將它們添加並返回正確的值。

三,的tableView致電詢問該行的實際單元格:

- (UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

在你的代碼,你只返回其具有由indoorZones數組填充數據的單元格,但您還需要提供爲特定區域/樓層正確配置的電池。同樣,您需要根據部分編號或行號來確定這一點。

- (void)tableView:(UITableView *)tableView 
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

在這種方法中,需要更新您的數據源所用:

最後,當你點擊一個排,tableview中通過調用下面的方法告訴你通過之前的功能,以便當他們再次被調用時,他們將提供正確的數據。您的數據源必須以您希望查看錶格視圖的方式進行鏡像。就你而言,你有一個名爲indoorZones的數組。如果你只是想顯示一個區域列表,這是你開始做的事。但是,如果要添加更多行,則需要先將更多行添加到數據源中,以便當tableView啓動此過程時,它已經存在。

如果你希望一切都停留在一個部分,那麼我會想出一個數據源,它可以包含兩種類型的行,並且能夠區分它們以便cellForRowAtIndexPath可以創建適當類型的單元並返回它。

如果你想要兩個部分,那麼我會爲第二部分添加第二個數組(因爲它不是同一類型的數據),並根據所需的數組返回適當的值用於該部分。

我希望這有助於!

+0

我已經做了一些更改和更新上面的代碼.......我收到以下錯誤.. ....集合<__ NSArrayM:0x4b1d550>在枚舉時發生了變化。在控制檯中,我可以看到行和子行得到添加,但不顯示 –

+0

當您使用快速枚舉時,不能更改數組。既然你實際上並沒有使用來自indoorZones的對象,只要將'for(NSArray * count in self.indoorZones)'改成'for(int x = 0; x <[self.indoorZones count]; x ++)''。 – lnafziger

+0

嗨...我已經完成了我的應用程序中的所有模塊,除了上面的tableview事情。每當我收到新的錯誤。我想與你分享這個模塊文件,所以你可以給你的郵件ID。 –