gpt4 book ai didi

iOS - 带有数组数据的简单表格部分

转载 作者:行者123 更新时间:2023-12-01 17:54:39 24 4
gpt4 key购买 nike

我刚开始使用 iOS/Xcode,并且已经在谷歌搜索/Youtube 上搜索了一个小时,但找不到匹配的教程。我现在要做的就是显示一个表格,其中包含按正文部分(部分)分组的练习(行)列表。 bodypart 部分永远不会改变,但用户将能够将自定义练习添加到 bodypart。

现在,我假设我需要一个用于各个部分的数组以及一个用于练习的数组……创建这些很简单。我遇到了将练习分配给特定部分的问题。这是一个错误代码的示例,当渲染时,它会在两个部分下显示两个练习......而且表中没有生成任何部分名称,所以我也不确定它在哪里发挥作用。

这是结果的屏幕截图(作为旁注,不知道为什么我的导航 Controller 没有渲染):http://i.imgur.com/icoJgEq.jpg

创建单个项目:

@property NSString *exerciseName;
@property NSString *exerciseCategoryName;

创建/分配数组:
@property NSMutableArray *exerciseCategories;
@property NSMutableArray *exercises;

self.exerciseCategories = [[NSMutableArray alloc]init];
self.exercises = [[NSMutableArray alloc]init];

用一些默认数据填充数组:
- (void)loadInitialData {
FNTExerciseCategories *category1 = [[FNTExerciseCategories alloc]init];
category1.exerciseCategoryName = @"Chest";
[self.exerciseCategories addObject:category1];
FNTExerciseCategories *category2 = [[FNTExerciseCategories alloc]init];
category2.exerciseCategoryName = @"Biceps";
[self.exerciseCategories addObject:category2];

FNTExercises *exercise1 = [[FNTExercises alloc]init];
exercise1.exerciseName = @"Bench Press";
[self.exercises addObject:exercise1];
FNTExercises *exercise2 = [[FNTExercises alloc]init];
exercise2.exerciseName = @"Barbell Curl";
[self.exercises addObject:exercise2];
}

加载数据:
[self loadInitialData];

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [self.exerciseCategories count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.exercises count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ExercisePrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
MFTExercises *exercise = [self.exercises objectAtIndex:indexPath.row];
cell.textLabel.text = exercise.exerciseName;
return cell;
}

非常感谢任何可以插话的人!

最佳答案

实际上在 tableView:numberOfRowsInSection:您正在返回整个 exercises 的计数大批。因此,对于您的示例数据,每个部分将有两行。尝试为每个部分制作一系列练习,然后编写如下代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return [self.chestExercises count];
}
else if (section == 1) {
return [self.bicepsExercises count];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ExercisePrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
MFTExercises *exercise;
if (indexPath.section == 0) {
exercise = [self.chestExercises objectAtIndex:indexPath.row];
}
else if (indexPath.section == 1) {
exercise = [self.bicepsExercises objectAtIndex:indexPath.row];
}
cell.textLabel.text = exercise.exerciseName;
return cell;
}

在这种情况下, chestExercises数组将只包含“卧推”练习和 bicepsExercises只会包含“杠铃弯举”-练习。所以你会得到每节一排。

为了实现这些部分具有标题,您需要实现该方法
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
return [self.exerciseCategories objectAtIndex:section];
}

它根据存储在数组中的名称为部分提供标题。

构建数据源的更复杂的方法是创建 NSDictionary以节名作为键(正文部分),值是包含正文部分练习的数组。例如,如果您的类别只是字符串,您可以使用示例数据构建这样的字典(为了演示,我添加了另一个练习):
FNTExerciseCategories *category1 = [[FNTExerciseCategories alloc]init];
category1.exerciseCategoryName = @"Chest";
[self.exerciseCategories addObject:category1];
FNTExerciseCategories *category2 = [[FNTExerciseCategories alloc]init];
category2.exerciseCategoryName = @"Biceps";
[self.exerciseCategories addObject:category2];

FNTExercises *exercise1 = [[FNTExercises alloc]init];
exercise1.exerciseName = @"Bench Press";
FNTExercises *exercise2 = [[FNTExercises alloc]init];
exercise2.exerciseName = @"Barbell Curl";
FNTExercises *exercise3 = [[FNTExercises alloc]init];
exercise3.exerciseName = @"Another Exercise";

// the instance variable self.exercises is a NSMutableDictionary now of course
self.exercises = [[NSMutableDictionary alloc] init];
exercises[category1.exerciseCategoryName] = @[exercise1];
exercises[category2.exerciseCategoryName] = @[exercise2, exercise3];

这里的优点是您现在拥有一个包含所有数组的字典,其中包含所有数据。因此,当您添加更多数据时,您不必更改 tableView 数据源的实现。顺便说一句,我正在为字典和数组使用现代 Objective-C 语法。

创建了这样的字典后,您可以像这样简单地实现 TableView 数据源:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.

// This gives the name of the category at the current section.
// It is then used as a key for the dictionary.
NSString *currentCategory = [[self.exerciseCategories objectAtIndex:section] exerciseCategoryName];
return [self.exercises[currentCategory] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ExercisePrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
NSString *currentCategory = [[self.exerciseCategories objectAtIndex:indexPath.section] exerciseCategoryName];

MFTExercises *exercise = [self.exercises[currentCategory] objectAtIndex:indexPath.row];

cell.textLabel.text = exercise.exerciseName;
return cell;
}

使用 NSDictionary可能会或可能不会使您的应用程序受益,但您不必为您拥有的每个 body 部位创建一个数组作为实例变量。将单个字典保存到磁盘以保持持久性也可能更容易。

关于iOS - 带有数组数据的简单表格部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20108846/

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