gpt4 book ai didi

ios - 无法将行添加到 UITableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:26:02 25 4
gpt4 key购买 nike

我是 iOS 编程新手,在向 UITableView 对象添加新单元格时遇到问题。我正在使用一个 Storyboard,其中一个场景是一个 UIViewController,它有几个 subview :文本字段、一个 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];
}

(…)

同样在 Storyboard 中,我将 outlets delegate 和 dataSource 设置为 Controller View 。

有什么想法吗?

问候,琼巴

最佳答案

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

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

在 UIViewController 标题中我有一行

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

在实现中我有注释行

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

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

Outlets: dataSource, delegate --> UIViewController

引用 socket :UITableView --> UIVIewController

就是这样!!

关于ios - 无法将行添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9330011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com