【问题标题】:Not possible to add row to a UITableView无法向 UITableView 添加行
【发布时间】:2012-03-08 22:57:44
【问题描述】:

我是一名 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];
}

(…)

同样在情节提要中,我已将 outlets delegate 和 dataSource 设置为控制器视图。

有什么想法吗?

问候, 琼巴

【问题讨论】:

  • 你的意思是你可以添加最初的行但之后不能添加?

标签: ios uitableview storyboard


【解决方案1】:

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

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

在 UIViewController 标题中我有一行

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

在实现中我已经注释了行

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

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

Outlets:dataSource、delegate --> UIViewController

引用出口:UITableView --> UIVIewController

就是这样!!

【讨论】:

    【解决方案2】:

    您可以将表格视图设置为静态内容。在 Storyboard 中选择 UITableView,然后在屏幕右侧菜单的“Attributes Inspector”部分中选择“Table View”标题下的“Content”字段,并将值设置为“Dynamic Prototypes”。

    清晰的截图:

    这个小技巧在我刚开始的时候好几次让我出局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 2021-07-06
      • 2012-02-19
      • 1970-01-01
      相关资源
      最近更新 更多