不可能将行添加到UITableView

我是一个新手程序设计的iOS,并且在将一个新单元添加到UITableView对象时遇到问题。 我正在使用故事板,其中一个场景是一个UIViewController,它有几个子视图:textfields,tableview等。我打算从细节场景中添加行到这个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];
}

(…)

另外在故事板中,我已经设置了网点委托和数据源作为控制器视图。

任何想法 ?

问候,JoanBa


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

我回顾了故事板中的UITableView设置,它显然是正确的,但我已经删除它们并重新创建。 现在它可以工作。

在UIViewController头我有这条线

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

并在实施中我已经评论线

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

当然,在故事板中,我已经为UITableView定义了以下关系:

出口:数据源,委托 - > UIViewController

引用outlet:UITableView - > UIVIewController

而已 !!


您可能将表格视图设置为静态内容。 在故事板中选择UITableView,并在屏幕右侧菜单的“属性检查器”部分选择“表视图”标题下的“内容”字段,并将该值设置为“动态原型”。

屏幕截图清晰度:

在这里输入图像描述

当我刚出发的时候,这个小窍门几次让我感觉到了。

链接地址: http://www.djcxy.com/p/87723.html

上一篇: Not possible to add row to a UITableView

下一篇: NSObject loading UIView with nib but fileowner is UIViewController