2012-02-10 109 views
1

請查看我的代碼,我正在使用它在我的表視圖添加運行時添加動態單元格。在選擇我的表格視圖時,我稱這種方法。問題在表視圖中添加動態單元格?

- (void) allServersFound 
{ 
    // called from delegate when bonjourservices has listings of machines: 
    NSArray *newPosts = [[NSArray alloc]initWithObjects:@"A", nil]; // NSArray of machine names; 

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil]; 

int i = 0; 
for (NSArray *count in newPosts) 
{ 
    [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; 
} 

[[self tblHome] beginUpdates]; 
[[self tblHome] insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationNone]; 
[[self tblHome] endUpdates]; 

[tempArray release]; 
} 

但是,這給了我以下在運行時異常: *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是:「 - [NSIndexPath _fastCStringContents:]:無法識別的選擇發送到實例0x4e0e130

回答

1

首先,你用字符串初始化tempArray這樣的:

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil]; 

然後添加indexPaths升IKE在此:

[tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; 

所以你傳遞給insertRowsAtIndexPaths方法數組包含字符串和indexPaths對象。我認爲這是例外的原因。

0

您的tmparray包含NSString值以及IndexPath s,請嘗試先刪除NSString s。 嘗試與線

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects:nil]; 
1

更換線

NSMutableArray *tempArray = [[NSMutableArray alloc] initWithObjects: @"A",@"B",@"C",@"D", nil]; 

正如其他人所指出的,你tempArray包含NSStringNSIndexPath對象的混合。這是你做任何事情之前需要解決的最明顯的問題。您可以使用[NSMutableArray array]類方法(它是自動發佈的,因此您需要在方法結束時刪除對[tempArray release]的調用)。

一個不太明顯的一點是,你打電話之前insertRowsAtIndexPaths您的UITableView 模型必須進行更新,否則,你會得到一個更明顯的地方另一個異常。

相關問題