【发布时间】: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