Iphone 为什么我的NSMUTABLEARRY是“;“半死不活”;在cellForRowAtIndexPath中?
在rootViewController.h中,我有一个属性NSMutableArray mainList:Iphone 为什么我的NSMUTABLEARRY是“;“半死不活”;在cellForRowAtIndexPath中?,iphone,objective-c,ios,uitableview,nsmutablearray,Iphone,Objective C,Ios,Uitableview,Nsmutablearray,在rootViewController.h中,我有一个属性NSMutableArray mainList: @interface RootViewController : UITableViewController { NSMutableArray *mainList; } @property (nonatomic, retain) NSMutableArray *mainList; @property (nonatomic, retain) IBOutlet DetailsVi
@interface RootViewController : UITableViewController {
NSMutableArray *mainList;
}
@property (nonatomic, retain) NSMutableArray *mainList;
@property (nonatomic, retain) IBOutlet DetailsViewController *detailsController;
在m文件中,加载时,以下各项工作正常:
- (void)viewDidLoad
{
self.mainList = [[NSArray alloc] initWithObjects:@"Country1", @"Contry2", nil];
}
通过类填充数组也可以正常工作(第一次加载时):
(调试器显示数组中正确的数据)
滚动时,应用程序在设置单元格标签时崩溃:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.mainList objectAtIndex: [indexPath row]];
return cell;
}
在调试器中,数组仅填充1-9,而不是最多19。10-19含有奇怪的物体。什么会侵蚀我的阵列???尝试更改
self.mainList = myInnehall.theMainList;
到
您是否也可以将NSLog([self.mainList description])
放在您的cellForRowAtIndexPath中并发布结果
PS:您在属性声明中有NSMutableArray,并将其初始化为NSArray?首先,必须正确初始化NSMutableArray属性,如:
self.mainList=[[NSMutableArray alloc]init]代码>
(不是作为NSArray
)
然后,您将属性mainList
指向myInnehall.theMainList
并在之后释放它,这就是导致崩溃的原因
尝试将myInnehall
项目添加到您的main列表中
[self.mainlistaddobjectsfromarray:myInnehall.theMainList]代码>您发布的所有代码看起来都不是该问题的根源。当您使用第一种填充数组的方法时,会发生任何奇怪的情况。您只在按类填充时提到了一个问题。问题可能来自您分配、填充和发布myInnehall.theMainList的代码。谢谢您,Kashiv,问题是通过复制而不是指向来解决的。PS:请不要复制您的对象。。。除非您非常清楚NSArray如何实现NSCopying协议,否则只需将现有项添加到阵列中即可。
self.mainList = myInnehall.theMainList;
self.mainList = [myInnehall.theMainList copy];