2012-01-04 11 views
2

StoryboardsとObjective-Cのコーディングの違いを理解するのに苦労しています。オブジェクトをストーリーボードにドラッグするか、Objective-C内の新しいビューでコーディングすることによって、UITableViewを作成することができます。プログラムでUITableViewを作成し、Xcode 4.2でNSArrayを実装する方法は?

問題は、ストーリーボードを可能な限りスリムに保つことです。だから私はビルドし、5つの文字列のNSArrayでUITableViewを設定しようとしている。私のコードは、コンパイルエラーを返す前に1行しか実行されません...私はプロジェクト全体をスクラップし、新しく始めるつもりです。

新しいXcode 4.2/iOS5/Storyboardsをよく知っている方が、UITableViewを構築するための合理的なソリューションを提供できる場合は、非常に感謝します。私はこれが基本的な仕事であることを知っています。それがなぜ始まってとてもイライラしているのですか?私はテーブルビューを動作させることができますが、#X個の行を動的に塗りつぶして作成する配列を取得できないようです...

これ以上の情報を提供できるかどうか教えてください。私は、できるだけ簡単なことを試してみた - ちょうどArrayでテーブルビューが作業を取得して移入する必要があります:)

EDIT - ここではあなたが私がでてる場所をチェックアウトしてダウンロードすることができます私のproject source codeです。

+0

テーブルビュークラスのサンプルコードはありますか? UITableViewControllerのサブクラスにする方が簡単です。 Plsには、ヘッダーファイルと実装ファイルの両方が含まれます。 – chourobin

+0

@chourobin確かに、自分のxcodeプロジェクトのソースをオリジナルの投稿に添付しました。 – Jake

+0

@JakeRocheleau - ちょっと好奇心が強い、なぜあなたのストーリーボードをできるだけスリムにしたいですか?私は、インターフェースビルダーのようなストーリーボードでは、グラフィカルに作業を進めることでコードを軽くでき、APIの変更による将来のエラーを防ぐことができたという印象を受けました。 – 5StringRyan

答えて

3

理由はストーリーボードにあなたがダイナミックプロトタイプの代わりに、静的な細胞にテーブルビューを変更する必要があることです。 何らかの理由で、静的セルがデフォルト設定です。ストーリーボードのハングアップを取得したら、特にテーブルビューを扱うときには素晴らしいことです。最初のビューは、MasterviewControllerをRootViewControllerとして持つNavigationControllerとして設定されているので、firstViewとしてロードされています。 MainStoryboardでTableViewをクリックし、CelsをDynamic Prototypeに変更するか、ストーリーボードで作成した静的なものを使用します。ストーリーボードのテーブルビューでカスタムセルを作成することができます。注意すべき点は、再利用識別子がストーリーボードとTableViewControllerで同じ名前に設定されていることです。 静的セルの数を常に同じ数値にすることもできます。

+1

何らかの理由で、静的セルがデフォルトの設定です。ストーリーボードのハングアップを取得したら、特にテーブルビューを扱うときには素晴らしいことです。 –

+0

男は本当にありがとう!正直なところ、この問題は私をあまりにも落胆させています。私は数週間それを理解することができなかった、そして今私はついに私のプロジェクトで前進することができます。 「再利用識別子」が何であるか、そしてストーリーボードでこの値をどこに設定すべきかを説明できますか? – Jake

+0

なぜデフォルトは静的なのですか?ほとんどのテーブルビューはダイナミックプロトタイプです。 –

3

ここでは、サンプルコードからNSArray(NSMutableArray)を取り込んだUITableViewControllerをサブクラス化した簡単なサンプルを示します。それはストーリーボードを使用していませんが、あなたはコメントでOKだと言った。うまくいけば私のサンプルコードはあなたを助けてくれるでしょう

ヘッダー:

@interface MainTableViewController : UITableViewController 
{ 
    NSMutableArray *_items; 
} 

@end 

実装:それはクラッシュ

@implementation MainTableViewController 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
#pragma mark - 
#pragma mark Lifetime 
#pragma mark - 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) 
    { 
     // datastore 
     _items = [[NSMutableArray alloc] init]; 
     for (int index=0; index < 5; index++) 
     { 
      [_items addObject:[NSString stringWithFormat:@"item #%d", index]];    
     } 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [_items release]; 
    [super dealloc]; 
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
#pragma mark - 
#pragma mark Table View DataSource 
#pragma mark - 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // a typical table has one section 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // number of rows 
    return [_items count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // NSIndexPath contains an array of indexes. For UITableView: 
    // indexAtPosition:0 is the section number 
    // indexAtPosition:1 is the row number 

    // create an identifier for this type of cell 
    static NSString *CellIdentifier = @"Cell"; 

    // get a cell of this type from the re-use queue or create one 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    NSString *title = [_items objectAtIndex:[indexPath indexAtPosition:1]]; 
    [[cell textLabel] setText:title]; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

    return cell; 
} 


// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 


// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     // Delete the row from the data source 
     NSLog(@"delete section: %d rol: %d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]); 
     [_items removeObjectAtIndex:[indexPath indexAtPosition:1]]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) 
    { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
     NSLog(@"insert section: %d rol: %d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]);   
    } 
} 

// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
    NSString *fromItem = [_items objectAtIndex:[fromIndexPath indexAtPosition:1]]; 
    [_items removeObjectAtIndex:[fromIndexPath indexAtPosition:1]]; 
    [_items insertObject:fromItem atIndex:[toIndexPath indexAtPosition:1]]; 
} 

// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return YES; 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 
} 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
#pragma mark - 
#pragma mark UITableViewDelegate 
#pragma mark - 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"selected section: %d rol: %d", [indexPath indexAtPosition:0], [indexPath indexAtPosition:1]); 

    // get the selected cell 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

    // navigate to detail 
    DetailedTableViewController *detailedView = [[DetailedTableViewController alloc] init]; 
    [[self navigationController] pushViewController:detailedView animated:YES]; 
} 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
#pragma mark - 
#pragma mark View lifecycle 
#pragma mark - 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    [[self navigationItem] setRightBarButtonItem: [self editButtonItem]]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 
関連する問題