2012-02-26 9 views
0

私はiOS devを使い慣れていません。インスタンス変数がUITableViewControllerでどのように解放されるのですか?

私はUITableViewControllerのviewDidLoadメソッドでデータを配列にロードしています。ここでのNSLogでコード

- (void)viewDidLoad 
{ 
[super viewDidLoad];  
// Get Data 
PANewsListModel *info = [PADataSource getNewsList]; 
_data = [info.news]; 

NSLog(((PANewsModel *)[_data objectAtIndex:1]).title); 

UIImageView *bkgrd = [[UIImageView alloc]initWithImage:[UIImage imageNamed:BACKGROUND_IMAGE]]; 
self.tableView.backgroundView = bkgrd; 
self.tableView.rowHeight = 100; 
} 

は、データがアクセス可能である呼び出しです。

しかし、TableCellに値を設定するメソッドでは、メモリ例外が発生します。コードは次のとおりです

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSLog(@"tableView"); 
    static NSString *CellIdentifier = @"NewsCellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"PANewsCellIB" owner:self options:nil]; 
     cell = tableCell; 
     self.tableCell = nil; 

     PANewsModel *newsItem = (PANewsModel *) [self.data objectAtIndex:1]; 

     UILabel *title = (UILabel *)[cell viewWithTag:101]; 
     title.text = newsItem.title; // This instruction gives EXC_BAD_ACCESS 

    } 

    [cell sizeToFit]; 
    return cell; 
} 

これらのメソッド間でインスタンス変数を割り当てることができますか?

UPDATE:詳細

これは、それはちょうどあなたが投稿したコードで伝えるのは難しいデータ

.h 
@property (nonatomic, retain) NSMutableArray *data; 
.m 
@synthesize data = _data; 

そしてPANewsModelクラス

@interface PANewsModel : NSObject 

@property (nonatomic, retain) NSString *title; 
@property (nonatomic, retain) NSString *shortDescription; 
@property (nonatomic, retain) NSString *description; 
@property (nonatomic, retain) NSDate *date; 
@property (nonatomic, retain) UIImage *photo; 

-(id) initWithTitle: (NSString *) t 
      shortDesc: (NSString *) s 
     description: (NSString *) d 
       date: (NSString *) date 
       image: (UIImage *)image; 

@end 
+0

'_data'と' self.data'はどのように宣言されていますか? – sch

+2

'_data = [info.news];'は無効な構文です。それはタイプミスですか? –

+0

'cell = tableCell; self.tableCell = nil; 'ちょうどあなたがivarのオブジェクトを別のポインタに割り当てて、セッターを使って割り当てたオブジェクトを取り除いているようです。 'tableCell'とは何ですか? –

答えて

0

の宣言ですが、 viewDidLoad_dataオブジェクトを保持する必要があります。試してみてください:

PANewsListModel *info = [PADataSource getNewsList]; 
_data = [info.news retain]; 

deallocの方法で必ずリリースしてください。

+0

また、retainの型であればsetterを使うことができます: 'self.data = ???' – sch

+0

そうです。しかし、彼はヘッダーを投稿しなかったので、これはどちらでもうまくいく。 – edc1591

+0

私はあなたのawnserを試しましたが、それは動作しませんでした、私はもっと多くの情報で質問を更新しました、おそらく私の間違いがあります。 –

関連する問題