2010-01-13 55 views
5

我試圖複製NSMutableArray到另一個,但它並沒有顯示我什麼在UITableView複製NSMutableArray的另一個

NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:@"one",@"two"]; 

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil]; 

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

[self.list addObjectsFromArray:myArray]; 

沒有顯示出來!哪裏不對?

它崩潰了我的應用程序,因爲我沒有零在我的NSMutableArray如何添加零它? addobject:nil不起作用崩潰的應用程序:

static NSString * DisclosureButtonCellIdentifier = 
@"DisclosureButtonCellIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
         DisclosureButtonCellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier: DisclosureButtonCellIdentifier] 
      autorelease]; 
} 
NSUInteger row = [indexPath row]; 

NSString *rowString =nil; 

rowString = [list objectAtIndex:row]; 


cell.textLabel.text = rowString; 

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
[rowString release]; 
return cell; 
+2

爲何要顯示什麼?發佈您的cellForRowAtIndexPath:方法的代碼 – Morion 2010-01-13 14:08:00

+0

更新如何添加nil到添加對象的NSMutableArray?它崩潰我的應用程序在cellForRowAtIndexPath – stefanosn 2010-01-13 15:05:05

回答

19

你的初始調用的Alloc一個NSMutableArray很可能會崩潰,因爲你沒有在你的參數列表零終止。

此外,你有一個局部變量,列表和一個屬性列表。確保你正在實例化你的想法。您可能需要這麼做:

NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:@"one",@"two", nil]; 

NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil]; 

self.list = [[NSMutableArray alloc] init]; 

[self.list addObjectsFromArray:myArray]; 
+0

NSMutableArray * myArray = [[NSMutableArray alloc] initWithArray:objectsToAdd]; initWithObjects需要單個對象,而不是可變數組。此外myArray是不可變的。 – Hahnemann 2012-11-25 18:46:13

0

的問題可能是的list局部聲明中第4間的衝突與屬性。

1

存在一些問題......一個問題是您正在使用'initWithObjects'並添加上一個數組。這看起來像是不想要的行爲,因爲你很可能想將字符串@「one」和@「two」添加到數組中。您很可能打算使用initWithArrayaddObjectsFromArray。 (這樣做,將添加NSMutableArray(不是它的對象)到列表中)

第二個問題,當您使用initWithObjects時,您需要列出每個對象,然後用零結束列表值。 (docs)換句話說,你需要使用...

NSMutableArray *objectsToAdd = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", nil]; 
2

這可以幫助你:

NSMutableArray *result = [NSMutableArray arrayWithArray:array]; 

NSMutableArray *result = [array mutableCopy]; //recommended 
相關問題