2012-01-11 5 views
3

に送信します。現時点では、私がシミュレータを実行すると、問題のtableViewは5行(これは正しい)が表示されますが、すべての行は同じ文字列です(これは明らかに正しくありません)。表示されている文字列は、NSMutableArrayで定義されているLASTオブジェクトのものです。ここでは、コードです:のaddObject:NSMutableArrayのを、私はNSMutableArrayのから移入だのtableViewを作成しようとしていますXcodeの4.2 iPhoneプロジェクト</p> <p>テーブルビュー

- (void)viewDidLoad 
{ 

[super viewDidLoad]; 

codes = [[NSMutableArray alloc] init]; 
TableList *listItem = [[TableList alloc] init]; 

[listItem setCodeTitle:@"Test Name 1"]; 
[listItem setCodeNumber:@"101"]; 
[listItem setCodeDescription:@"This is the description for 101."]; 
[codes addObject:listItem]; 

[listItem setCodeTitle:@"Test Name 2"]; 
[listItem setCodeNumber:@"102"]; 
[listItem setCodeDescription:@"This is the description for 102."]; 
[codes addObject:listItem]; 

[listItem setCodeTitle:@"Test Name 3"]; 
[listItem setCodeNumber:@"103"]; 
[listItem setCodeDescription:@"This is the description for 103."]; 
[codes addObject:listItem]; 

[listItem setCodeTitle:@"Test Name 4"]; 
[listItem setCodeNumber:@"104"]; 
[listItem setCodeDescription:@"This is the description for 104."]; 
[codes addObject:listItem]; 

[listItem setCodeTitle:@"Test Name 5"]; 
[listItem setCodeNumber:@"105"]; 
[listItem setCodeDescription:@"This is the description for 105."]; 
[codes addObject:listItem]; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//#warning Potentially incomplete method implementation. 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//#warning Incomplete method implementation. 
// Return the number of rows in the section. 
return [codes count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"CodeCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

// Configure the cell... 
TableList *current = [codes objectAtIndex:indexPath.row]; 
cell.textLabel.text = [current codeTitle]; 

return cell; 
} 

私は、私は非常に少しを持っている...これは私よりも多くの経験を持つ人には痛いほど明白であると思います。

答えて

3

まったく同じオブジェクトをアレイに5回保存しています。 -addObject:は、渡されたオブジェクトをコピーするという誤解の下にあるようですが、それはできません。簡単な修正は、あなたのTableListクラスが<NSCopying>に準拠仮定し、当然のことながら、

[codes addObject:[[listItem copy] autorelease]]; 

これがあると言って、あなたの-addObject:ラインを変更することです。そうでない場合は、そうしたいと思うでしょう。そのクラスでコピーをサポートしたくない場合は、毎回新しいTableListオブジェクトを作成する必要があります。

また、ARCを使用している場合を除き、TableListオブジェクトがリークしています。 -copyをサポートするために


、あなたは(ゾーン引数は無視されなければならない歴史的な好奇心ですが)メソッド-copyWithZone:を宣言したプロトコルである、<NSCopying>に準拠する必要があります。これを行うには、あなたの@interface行を変更し

@interface TableList : NSObject <NSCopying> 

に見えるようにして、-copyWithZone:

- (id)copyWithZone:(NSZone *)zone { 
    // If our superclass conformed to <NSCopying> we'd call [super copy] 
    // But it doesn't, so alloc/init a new instance 
    TableList *newObj = [[TableList alloc] init]; 
    newObj.codeTitle = self.codeTitle; 
    newObj.codeNumber = self.codeNumber; 
    newObj.codeDescription = self.codeDescription; 
    return newObj; 
} 

メソッドを実装するこれはただのサンプル実装です。これらの3つのプロパティはオブジェクト上の唯一の値であると仮定し、単にプロパティに割り当てることは適切です。あなたはオーバーコピーする必要がある内部アイバーズを持っている場合は、NSMutableArrayのを使用している場合、あなたは、また

newObj->ivar = copyIvar(self->ivar); // self->ivar is the same as just plain ivar 
+0

ありがとうございます、私はARCを使用していますので、そのクラスをリリースする必要はありません。また、ARCを使用しているため、自分のコードで自動解放機能を使用できなくなります。私はaddObject行を次のように置き換えようとしました: [code addObject:[listItem copy]]; tableViewが読み込もうとしたときにSIGABRTが表示されます。 おそらく、これは私のクラスがコピーをサポートしていないからでしょうか? –

+0

@Paul:ARCでは 'autorelease'を省略できます。 SIGABRTはどのラインですか? 'TableList'が' 'に準拠していない場合、' -copy'の呼び出しは例外をスローします。 –

+0

私のクラスTableListはNSObjectのサブクラスとして作成されました。私はこれがコピーを使用できると信じています。 –

0

のようなものを使用してIVARのアクセスを得ることができ、NSArrayのに連鎖してみてください。

関連する問題