我是iOS的新手,在将新单元格添加到UITableView对象时遇到问题。我正在使用一个故事板,其中一个场景是一个UIViewController,它具有几个子视图:文本字段,表视图等。我打算从详细场景中向该tableView添加行。

我最初可以向表中添加行,但之后不能添加行。当我按下按钮添加行时,我将方法称为“[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];
}

(…)

同样在情节提要中,我将出口代理和dataSource设置为控制器视图。

任何想法 ?

问候,
琼巴

最佳答案

我已经解决了这个问题。我发现使用调试器发现,reloadData方法的调用不同于在viewDidLoad中初始化的UITableView。

我查看了情节提要中的UITableView设置,这些设置显然是正确的,但是我已将其删除并重新创建。现在可以了。

在UIViewController标头中,我有一行

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

在实施过程中,我评论了以下内容
//self.stepsListTableView.delegate = self;
//self.stepsListTableView.dataSource = self;

而且,当然,在情节提要中,我为UITableView定义了以下关系:

出口:dataSource,委托-> UIViewController

引用出口:UITableView-> UIVIewController

而已 !!

10-05 20:15
查看更多