标题更改后重复上一个 UITableViewCell [英] Previous UITableViewCell repeating after header change

查看:24
本文介绍了标题更改后重复上一个 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建自己的个人预算应用,该应用将商店、花费的金额、日期等数据放到表格中.我正在按 strftime %W 对条目进行排序.我按 %W 排序的数据字典是:

I'm creating my own personal budget app that is taking data such as store, money spent, date, etc. onto a table. I'm sorting the entries by strftime %W. My dictionary of the data sorted by %W is:

_results =

{ 
  46 =  (
            {
              DATE = "11/13/2019";
              ID = 10;
              PLACE = Test;
              PRICE = "43.23";
            },
            {
              DATE = "11/15/2019";
              ID = 11;
              PLACE = OneMore;
              PRICE = "32.12";
            }
       );
  47 =  (
            {
              DATE = "11/18/2019";
              ID = 12;
              PLACE = Kroger;
              PRICE = "54.32";
            }
        );
}

我的 UITableView 设置是:

My UITableView setup is:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_resultsTitles count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [[_resultsTitles objectAtIndex:section] stringValue];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//     return self.arrFoodInfo.count;
    NSNumber *sectionTitle = [_resultsTitles objectAtIndex:section];
    NSArray *sectionAnimals = [_results objectForKey:sectionTitle];
    return [sectionAnimals count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Dequeue the cell.
    static NSString *identifier = @"idCellRecord";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    // Set the loaded data to the appropriate cell labels.
    cell.textLabel.text = [NSString stringWithFormat:@"%@ | $%@", [[self.arrFoodInfo objectAtIndex:indexPath.row] valueForKey:@"PLACE"], [[self.arrFoodInfo objectAtIndex:indexPath.row] valueForKey:@"PRICE"]];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Date: %@", [[self.arrFoodInfo objectAtIndex:indexPath.row] valueForKey:@"DATE"]];

    return cell;
}


运行我的应用程序时,表头是正确的,但是当在下一部分中添加"一个条目时,第一个条目会重复.


When running my app, the table headers are correct but when an entry is "added" in the next section, the first entry is duplicated.

无论如何要获得按周数分组的正确条目显示?

Is there anyway to get the correct entries showing grouped by the week number?

推荐答案

可以通过将以下内容添加到 cellForRowAtIndexPath 中来解决:

Was able to solve by adding the following into cellForRowAtIndexPath:

NSNumber *sectionTitle = [_resultsTitles objectAtIndex:indexPath.section];
NSArray *sectionResults = [_results objectForKey:sectionTitle];
NSString *cellData = [sectionResults objectAtIndex:indexPath.row];

现在看起来像:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Dequeue the cell.
    static NSString *identifier = @"idCellRecord";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }

    NSNumber *sectionTitle = [_resultsTitles objectAtIndex:indexPath.section];
    NSArray *sectionResults = [_results objectForKey:sectionTitle];
    NSString *cellData = [sectionResults objectAtIndex:indexPath.row];

    // Set the loaded data to the appropriate cell labels.
    cell.textLabel.text = [cellData valueForKey:YourValHere];

    return cell;
}

这篇关于标题更改后重复上一个 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆