1

テーブル ビューから別のテーブル ビューへ (Xcode 4.2 および iOS 5 を使用)。

FirstPage.h

#import "FavoritesController.h"

#import "Profiles.h"

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FavoritesController * favoriteview = [[FavoritesController alloc] init];
    [favoriteview setTitle:@"Favorites"];

    NSMutableArray * profiles = [[NSMutableArray alloc]init ];
    profiles = [NSMutableArray arrayWithCapacity:20];

    Profiles * profile = [[Profiles alloc]init];
    profile.profile_name = @"Woot";
    profile.biz_type_desc = @"Woot 1";
    profile.profile_address = @"123, woot";
    profile.profile_email = @"woot@woot.com";
    [profiles addObject:profile];
    profile=[[Profiles alloc]init];
    profile.profile_name = @"Jin-Aurora";
    profile.biz_type_desc = @"Software";
    profile.profile_address = @"682A";
    profile.profile_email = @"jin@jin.biz";
    [profiles addObject:profile];

    [self.navigationController pushViewController:favoriteview animated:YES];
    favoriteview.profilelist = profiles; 
}

FavoriesController.h

@interface FavoritesController : UITableViewController

@property(nonatomic,strong)NSMutableArray * profilelist;

@end

FavoriteController.m

 #import "FavoritesController.h"
 #import "Profiles.h"
 #import "ProfileCell.h"

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return [self.profilelist count];
}

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

    ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    Profiles * profile = [self.profilelist objectAtIndex:indexPath.row];

    cell.nameLabel.text = profile.profile_name;
    cell.biztypeLabel.text = profile.biz_type_desc;

    // Configure the cell...

    return cell;
}

ストーリーボード テーブル ビュー

これは私が得たエラーです

2012-02-08 22:28:37.719 テスト [4668:f803] キャッチされていない例外'NSInternalInconsistencyException' が原因でアプリを終了しています。

4

3 に答える 3

2

cellForRowAtIndexPath メソッドは再利用可能なセルをデキューしようとしますが、見つからない場合は作成しません (再利用できるセルがない場合に発生します)。

ProfileCell *cell = (ProfileCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
    cell = [[[ProfileCell alloc] initWithStyle:style reusueIdentifier:CellIdenfitier] autorelease];
于 2012-02-08T15:01:49.173 に答える
1

dequeueReusableCellWithIdentifier:とdequeueReusableCellWithIdentifier:forIndexPath:は異なるメソッドであることに注意してください。

以下のリンクが役立つ場合があります。

dequeueReusableCellWithIdentifier:forIndexPathでのアサーションの失敗:

于 2012-11-19T05:15:08.650 に答える
0

あなたがここで何をしようとしているのかよくわかりません:

NSMutableArray * profiles = [[NSMutableArray alloc]init ];
profiles = [NSMutableArray arrayWithCapacity:20];

最初の行は、プロファイルと呼ばれる新しい可変配列を作成します。2 行目は、容量 20 の自動解放された可変配列を作成し、それをプロファイルに割り当てます。したがって、基本的には、最初の行で作成した配列を覆い隠しています。あなたはどちらかを言うことができます

NSMutableArray *profiles = [[NSMutableArray alloc] initWithCapacity:20];

また

NSMutableArray *profiles = [NSMutableArray arrayWithCapacity:20];

@ wattson12 が述べたように、クラッシュしている理由は、作成されていないセルをデキューしているためです。常にセルをデキューしようとしますが、セルが存在しない場合は作成する必要があります。繰り返しますが、@ wattson12 はそのタスクに必要なコードを提供しています。

于 2012-02-08T15:15:02.530 に答える