1

我正在开发一个带有 tableView 控制器的项目,详细视图包含 CMMotionManager。当我打开 5 或 6 个 detailViews 时一切顺利,但过了一段时间应用程序变慢并最终崩溃。

在仪器上,唯一的泄漏是在 main.m 上,而且我必须说我正在使用 ARC,我无法解除或重新分配实例。

这是代码:首先是表格视图:

- (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;
}

和 detailViewdid 加载:

我没有收到错误或警告,什么都没有。就在我加载 6,7 个详细视图时,motionManagers 似乎停止工作.. 只是这个......我可以回到主视图并一次又一次地加载详细项目但运动管理器不起作用。这是我的详细视图的代码

@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];

     }];

`

4

2 回答 2

0

您应该检查dequeueReusableCellWithIdentifier. 如果它为 nil,则您可能在您不拥有的内存部分中设置属性。

请参考此文档dequeueReusableCellWithIdentifier如果您想跳过章节,请搜索)

于 2012-06-04T10:10:12.293 回答
0

您需要重复使用单元格。

以下是您应该如何正确执行此操作:

- (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,因此如果没有可用的 resubale 单元格,我们将初始化一个新单元格。

于 2012-06-04T11:10:15.920 回答