2012-02-17 10 views
2

私は初心者プログラミングのiOSです。新しいセルをUITableViewオブジェクトに追加する際に問題があります。私はストーリーボードを使用しています。シーンの1つは、テキストフィールド、テーブルビューなどの複数のサブビューを持つUIViewControllerです。詳細シーンからこのテーブルビューに行を追加しようとしています。UITableViewに行を追加することができません

最初はテーブルに行を追加できますが、後で行を追加することはできません。行を追加するためにボタンを押すと、メソッド '[self.stepsListTableView reloadData];が呼び出されます。メソッド ' - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section'を呼び出し、新しい配列要素を含む正しい値を返します。しかし、メソッド ' - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath'は、テーブルを更新するために呼び出されません。

私は間違っていることを理解していません。私のソースコードの

詳細:ストーリーボードでも

WorkoutDetailsViewController.h

(…) 
@interface WorkoutDetailsViewController : UIViewController <StepDetailsViewControllerDelegate, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, weak) id <WorkoutDetailsViewControllerDelegate> delegate; 
@property (nonatomic, strong) Workout *workout; 
(…) 
@property (nonatomic, retain) IBOutlet UITableView *stepsListTableView; 
(…) 

WorkoutDetailsViewController.m

(…) 
@synthesize stepsListTableView; 
(…) 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    addButton.enabled = FALSE; 

    workoutNameField.delegate = self; 
    if (self.workout == nil) { 
     self.workout = [[Workout alloc] init]; 
     self.stepsListTableView = [[UITableView alloc] init]; 
    } 
    self.stepsListTableView.delegate = self; 
    self.stepsListTableView.dataSource = self; 

} 

(…) 

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

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //return [self.workout.stepsList count]; 
    NSInteger counter = 0; 
    counter = [self.workout.stepsList count]; 
    return counter; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Set up the cell... 
    // Pending 

    return cell; 
} 

- (void)stepDetailsViewControllerDidDone:(StepDetailsViewController *)controller 
{ 
    [self.workout.stepsList addObject:controller.step]; 
    NSInteger counter = [self.workout.stepsList count]; 

    [self.stepsListTableView beginUpdates]; 

    NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:(counter-1) inSection:0]]; 
    [self.stepsListTableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop]; 

    [self.stepsListTableView endUpdates]; 

    [self.stepsListTableView reloadData]; 
} 

(…) 

、私がすべきセットアップ店デリゲートとデータソースを持っていますコントローラビュー。

よろしく、 JoanBa

+0

最初に行を追加できますが、それ以降は追加できないということはどういう意味ですか? – alecnash

答えて

1

私はこの問題を解決しました。デバッガを使用して、reloadDataがviewDidLoadで初期化されたものとは別のUITableViewに対して呼び出されたことがわかりました。

私はストーリーボードのUITableViewの設定を見直しましたが、実際には正しいですが、削除して再度作成しました。今それは動作します。UIViewControllerヘッダに

私はのUITableView以下の関係を定義したストーリーボードに、当然のことながら、ラインを

@property (nonatomic, retain) IBOutlet UITableView *stepsListTableView; 

および実装に私がコメントしている線

//self.stepsListTableView.delegate = self; 
//self.stepsListTableView.dataSource = self; 

を有しており:

アウトレット:データソース、デリゲート - > UIViewController

コンセントを参照する:UITableView - > UIVIewController

これはそれです!

0

あなたは、静的なコンテンツに設定されたテーブルビューを持つことができます。 StoryboardでUITableViewを選択し、画面の右側にあるメニューの "Attributes Inspector"セクションで、 "Table View"ヘッダーの下の "Content"フィールドを選択し、値を "Dynamic Prototypes"に設定します。明確にするため

スクリーンショット:私が始めたとき

enter image description here

この小さなトリックは数回私をつかまえました。

+0

私はすでに適切な値を持っていました:ダイナミックプロトタイプ。 – joanba

関連する問題