内存管理iOS开发应用程序没有';在一些细节项目之后,我无法工作
我正在使用tableView控制器处理一个项目,详细视图包含CMMotionManager。当我打开5或6个DetailView时,一切都进行得很顺利,但过了一段时间,应用程序运行缓慢,最终崩溃 在仪器上,唯一的漏洞是main.m,我必须说我使用的是ARC,我不能解除锁定或重新设置实例 代码如下: 首先是表视图:内存管理iOS开发应用程序没有';在一些细节项目之后,我无法工作,ios,memory,memory-management,uitableview,core-motion,Ios,Memory,Memory Management,Uitableview,Core Motion,我正在使用tableView控制器处理一个项目,详细视图包含CMMotionManager。当我打开5或6个DetailView时,一切都进行得很顺利,但过了一段时间,应用程序运行缓慢,最终崩溃 在仪器上,唯一的漏洞是main.m,我必须说我使用的是ARC,我不能解除锁定或重新设置实例 代码如下: 首先是表视图: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = @"Movement";//Master View Controller title bar
UIImage *image = [UIImage imageNamed:@"jg_navibar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
//Init the array with data
bodypartsMutableArray = [NSMutableArray arrayWithCapacity:26];
BodypartData *part1 = [[BodypartData alloc] init];
part1.bodypartname = @"Shoulder";
part1.movementname = @"Flexion";
part1.fullimageStartingPosition=[UIImage imageNamed:@"2_shoulder_flexion_end_position.jpg"];
part1.fullimageEndedPosition=[UIImage imageNamed:@"2_shoulder_flexion_end_position.jpg"];
part1.thumbimage=[UIImage imageNamed:@"1_shoulder_flexion_landmarks_thumb.jpg"];
[bodypartsMutableArray addObject:part1];
.........
}
然后单元格:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyBasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
BodypartData *part = [self.bodypartsMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text =[NSString stringWithFormat:part.movementname];
cell.detailTextLabel.text =[NSString stringWithFormat:part.bodypartname];
cell.imageView.image =part.thumbimage;
}
return cell;
}
并且DetailView确实加载了:
我没有收到任何错误或警告,什么都没有。就在我加载6,7个详细视图时,MotionManager似乎停止了工作。。就这个。。。我可以返回主视图并一次又一次地加载细节项,但运动管理器不起作用。这是我的详细视图的代码
@synthesize liveCounterLabel = _liveCounterLabel;
@synthesize resultLabel = _resultLabel;
@synthesize detailItem = _detailItem;
@synthesize imageView = _imageView;
@synthesize calculateButton = _calculateButton;
@synthesize motionManager =_motionManager;
@synthesize selectedImage=_selectedImage;
@synthesize unselectedImage=_unselectedImage;
@synthesize m11started=_m11started;
@synthesize m12started=_m12started;
@synthesize m13started=_m13started;
@synthesize m11ended=_m11ended;
@synthesize m12ended=_m12ended;
@synthesize m13ended=_m13ended;
@synthesize m11=_m11;
@synthesize m12=_m12;
@synthesize m13=_m13;
@synthesize queue=_queue;
@synthesize buttonCounter=_buttonCounter;
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.imageView.image = [self.detailItem fullimageStartingPosition];
NSString* longString = [[self.detailItem bodypartname ] stringByAppendingString: [@" " stringByAppendingString:[self.detailItem movementname]]];
self.navigationItem.title = [NSString stringWithFormat:longString];
//Backround Image code
[[self view] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]]];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Init motionManager object and set the Update Interval
_buttonCounter=0;
_motionManager = [[CMMotionManager alloc]init];
_motionManager.deviceMotionUpdateInterval=1/60; //60 Hz
[_motionManager startGyroUpdates];
if (!_queue){
_queue = [NSOperationQueue mainQueue];
}
if (_motionManager.gyroAvailable) {
_motionManager.gyroUpdateInterval = 1.0/60.0;
//[_motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
// withHandler: ^(CMDeviceMotion *motion, NSError *error)
[_motionManager startDeviceMotionUpdatesToQueue:_queue
withHandler: ^(CMDeviceMotion *motion, NSError *error)
{
CMAttitude *attitude = [[CMAttitude alloc] init];
attitude=motion.attitude;
//Calculation with rotationMatrix
_m11 = [NSString stringWithFormat:@"%.02f", attitude.rotationMatrix.m11];
_m12 = [NSString stringWithFormat:@"%.02f", attitude.rotationMatrix.m12];
_m13 = [NSString stringWithFormat:@"%.02f", attitude.rotationMatrix.m13];
}];
`您应该检查
dequeueReusableCellWithIdentifier
的结果。如果为nil,则可能是在不拥有的内存部分中设置属性
请参阅此部分(搜索
dequeueReusableCellWithIdentifier
,如果您想跳过章节)您需要重用单元格
以下是您应该如何正确执行此操作:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyBasicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
BodypartData *part = [self.bodypartsMutableArray objectAtIndex:indexPath.row];
cell.textLabel.text =[NSString stringWithFormat:part.movementname];
cell.detailTextLabel.text =[NSString stringWithFormat:part.bodypartname];
cell.imageView.image =part.thumbimage;
return cell;
}
看看我们如何初始化单元格?
我们要求tableview为我们提供一个在初始化之前就已经可用的单元格(这样我们就可以重用同一个单元格,而不会浪费内存来分配一个新的单元格)
如果tableView没有可用的单元格(它是第一次加载),它将返回nil,因此如果没有可用的重新打包单元格,我们将初始化一个新单元格。我更改了出列单元格的代码。。。但是没有任何改变你应该用你的新代码更新你的问题,并提供关于崩溃消息的详细信息它是否仍在内存不足?您是否在XCode中收到内存警告?请发布您的崩溃日志更新我的问题。。。但我没有任何警告或错误。。它只是停止计算旋转速度后,我加载了一些细节项目。。。有时ti会崩溃,崩溃会显示main()(罕见),然后它与内存无关。问题出在别的地方