gpt4 book ai didi

ios - 带有部分问题的 UITableView?

转载 作者:行者123 更新时间:2023-11-28 20:08:17 24 4
gpt4 key购买 nike

我还没有完全理解 UITableView 中的部分,但这是我到目前为止得到的:

我的问题:我怎样才能算出每个部分要放多少数据?

基本上,我有解析的 JSON 数据,然后将数据保存到 NSMutableArray 中。这是我现在用来制作这两个部分的代码:

- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0){
return @"Section 1";
}
else{
return @"Section 2";
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}

这非常有效。它向 TableView 添加了两个部分。我的问题是将某些数据放入每个部分。

在解析的 JSON 数据中,每个对象都有一个日期,我将其与当前日期进行比较。如果日期在今天之后,我将其添加到第 1 节。如果不是,那么我将其添加到第 2 节。

我是这样处理的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
... // Date Stuff

if (indexPath.section== 0 && isNewEvent) {
cell.textLabel.text = [[events objectAtIndex:indexPath.row] objectForKey:@"opponent"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Date: %@ | Time: %@", [[events objectAtIndex:indexPath.row] objectForKey:@"date"], [[events objectAtIndex:indexPath.row] objectForKey:@"time"]];
cell.detailTextLabel.textColor = [UIColor grayColor];

cell.imageView.image = [UIImage imageNamed:@"sports.png"];
return cell;
}
else
{
cell.textLabel.text = [[events objectAtIndex:indexPath.row] objectForKey:@"opponent"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"Date: %@ | Time: %@", [[events objectAtIndex:indexPath.row] objectForKey:@"date"], [[events objectAtIndex:indexPath.row] objectForKey:@"time"]];
cell.detailTextLabel.textColor = [UIColor grayColor];

cell.imageView.image = [UIImage imageNamed:@"sports.png"];
return cell;
}
}

我认为这非常有效,但我在两个部分都得到了重复数据。我相信这是因为这段代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
// return [events count];
}
else{
// return [events count];
}
}

在这个 CODE cellforrowatindexpath 完成之前,我不知道如何知道我将在每个部分中放入多少数据。

如果您需要更多代码,我将非常乐意提供给您。谢谢你帮助我!

最佳答案

您的所有处理都应在应用调用 cellForRowAtIndexPath 之前完成

您应用的流程应该是:

在您的 viewDidLoad 方法中,您应该解析所有数据。此时您应该知道哪些对象应该进入哪个部分。例如。可能有 2 个数组,第一个数组和第二个 2 数组。获取所有数据,遍历它,将对象添加到每个数组。

那么此时你可以说:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return [arrayOne count];
}
else{
return [arrayTwo count];
}
}

关于ios - 带有部分问题的 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21238134/

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