我正在尝试将NSMutableArray
复制到另一个,但它没有在UITableView
显示任何内容:
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:@"one",@"two"];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
NSMutableArray *list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
什么都没有出现!有什么问题?
它崩溃了我的应用,因为我的NSMutableArray
没有nil我怎么能添加nil? 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;
答案 0 :(得分:19)
您对alloc NSMutableArray的初始调用很可能会崩溃,因为您的参数列表中没有nil终结符。
此外,您还有一个本地变量,列表和属性列表。确保你实例化你的想法。您可能需要这样做:
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:@"one",@"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];
答案 1 :(得分:2)
这可能会对您有所帮助:
NSMutableArray *result = [NSMutableArray arrayWithArray:array];
或
NSMutableArray *result = [array mutableCopy]; //recommended
答案 2 :(得分:1)
有一些问题......一个问题是你正在使用'initWithObjects'并添加前一个数组。这似乎是不必要的行为,因为您很可能想要将字符串@“one”和@“two”添加到数组中。您很可能打算使用initWithArray
或addObjectsFromArray
。 (按照你的方式做,将NSMutableArray(不是它的对象)添加到列表中)
第二个问题,当您使用initWithObjects时,您需要列出每个对象,然后使用nil值终止列表。 (docs)换句话说,你需要使用......
NSMutableArray *objectsToAdd = [[NSMutableArray alloc] initWithObjects:@"One", @"Two", nil];
答案 3 :(得分:0)
问题可能是第4行的list
的本地声明与该属性冲突。