【问题标题】:Previous UITableViewCell repeating after header change标题更改后重复上一个 UITableViewCell
【发布时间】:2019-11-14 02:13:12
【问题描述】:

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

_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 设置是:

- (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;
}


运行我的应用程序时,表头是正确的,但是在下一节中“添加”一个条目时,第一个条目是重复的。

是否有正确的条目按周数分组显示?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    可以通过在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;
    }
    

    【讨论】:

      【解决方案2】:

      self.arrFoodInfo 来自哪里?

      我猜 self.arrFoodInfo 有这个数据:

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

      每一节的indexPath.row是不同的,当移动到下一节时,indexPath.row会重新开始到。 p>

      你应该更新绑定数据的方式或者重构self.arrFoodInfo列表,比如JSON中这样的例子:

      [
        [
          {
            "DATE": "11/13/2019",
            "ID": 10,
            "PLACE": "Test",
            "PRICE": "43.23"
          },
          {
            "DATE": "11/15/2019",
            "ID": 11,
            "PLACE": "OneMore",
            "PRICE": "32.12"
          }
        ],
        [
          {
            "DATE": "11/18/2019",
            "ID": 12,
            "PLACE": "Kroger",
            "PRICE": "54.32"
          }
        ]
      ]
      

      【讨论】:

      • 我能够解决这个问题。请检查我上面的回复。谢谢!
      猜你喜欢
      • 2017-05-18
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-02-11
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多