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 に使用可能なセルがない場合 (初めてロードする場合)、nil が返されるため、使用可能な resubale セルがない場合は、新しいセルを初期化します。

于 2012-06-04T11:10:15.920 に答える