0

一部のセクションには展開可能な行がある7つのセクションがあるUITableViewがあります。展開可能なセクションをクリックすると、0行目の画像が変更されますが、展開できない最後のセクションも画像が変更されますが、テーブルビューをシャッフルするとすべてが正常になります。何が起こっているのかを明確にするために、以下の画像を投稿します。

最初の画面

展開可能なセクションをクリックすると、他の行の画像も変更されます

別の展開可能なセクションがクリックされた同じ画面で、前の展開されたセクションにも影響します。

私のコードを投稿する以下

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell;

if(tableView == self.mWeekTable)
{
    cell = [self.mWeekTable dequeueReusableCellWithIdentifier:CellIdentifier];

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    myBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
    myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sub_cell.png"]];
    //myBackgroundView.backgroundColor = [UIColor colorWithRed:219.0/255.0 green:218.0/255.0 blue:224.0/255.0 alpha:1.0];
    cell.backgroundView = myBackgroundView;
    [cell addSubview:myBackgroundView];

    UILabel *lblDateTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 150, 30)];
    lblDateTitle.font = [UIFont fontWithName:@"Helvetica" size:22];
    lblDateTitle.textColor = [UIColor colorWithRed:169.0/255.0 green:168.0/255.0 blue:176.0/255.0 alpha:1.0];
    lblDateTitle.backgroundColor = [UIColor clearColor];

    UILabel *lblEName = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 170, 25)];
    lblEName.font = [UIFont fontWithName:@"Helvetica" size:15];
    lblEName.textColor = [UIColor colorWithRed:47.0/255.0 green:200.0/255.0 blue:247.0/255.0 alpha:1.0];
    lblEName.backgroundColor = [UIColor clearColor];

    UIImageView *imgTimer = [[UIImageView alloc]initWithFrame:CGRectMake(10, 26, 20, 22)];

    UILabel *lblTime = [[UILabel alloc]initWithFrame:CGRectMake(40, 25, 150, 25)];
    lblTime.font = [UIFont fontWithName:@"Helvetica" size:15];
    lblTime.textColor = [UIColor blackColor];
    lblTime.backgroundColor = [UIColor clearColor];

    UIImageView *imgCateg = [[UIImageView alloc]initWithFrame:CGRectMake(175, 26, 20, 22)];

    UILabel *lblCateg = [[UILabel alloc]initWithFrame:CGRectMake(200, 28, 90, 20)];
    //self.lblCateg.text = @"Category";
    lblCateg.font = [UIFont fontWithName:@"Helvetica" size:15];
    lblCateg.textColor = [UIColor blackColor];
    lblCateg.backgroundColor = [UIColor clearColor];



    NSString *weekDate = [[NSString alloc]init];

    UILabel *lblEventsCount = [[UILabel alloc]initWithFrame:CGRectMake(110, 16, 100, 20)];
    lblEventsCount.backgroundColor = [UIColor clearColor];
    lblEventsCount.textColor = [UIColor colorWithRed:47.0/255.0 green:200.0/255.0 blue:247.0/255.0 alpha:1.0];
    lblEventsCount.font = [UIFont fontWithName:@"Helvetica" size:18];

    int evntCnt;

    if(indexPath.section == 0)
    {
         evntCnt = [self.mArrDayOneEvents count];
        if(!isWeekListOne)
        {

            NSDateFormatter* df = [[NSDateFormatter alloc]init];
            [df setDateFormat:@"EEE, dd"];
            weekDate = [df stringFromDate:[self.mArrDates objectAtIndex:indexPath.section]];
            lblDateTitle.text = weekDate;

            if(evntCnt == 0)
            {
                myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_blank.png"]];
            }
            else{
                myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_bg+.png"]];
                lblEventsCount.text = [NSString stringWithFormat:@"%d Events", evntCnt];
            }
        }
        else
        {
            if(indexPath.row == 0)
            {
               [cell addSubview:lblEventsCount];
                NSDateFormatter* df = [[NSDateFormatter alloc]init];
                [df setDateFormat:@"EEE, dd"];
                weekDate = [df stringFromDate:[self.mArrDates objectAtIndex:indexPath.section]];
                lblDateTitle.text = weekDate;

                if(evntCnt == 0)
                {
                    myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_blank.png"]];
                }
                else{
                    myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_bg-.png"]];
                    lblEventsCount.text = [NSString stringWithFormat:@"%d Events", evntCnt];
                }
            }
            else{
                int ind = indexPath.row;
                ind--;
               // cell.textLabel.text = [self.mArrDayOneEvents objectAtIndex:ind];
                lblEName.text = [self.mArrDayOneEvents objectAtIndex:ind];
                imgTimer.image = [UIImage imageNamed:@"timer1.png"];
            }
        }
        }
    [cell addSubview:lblDateTitle];
    [cell addSubview:lblEName];
    [cell addSubview:lblCateg];
    [cell addSubview:lblTime];
    [cell addSubview:imgCateg];
    [cell addSubview:imgTimer];
    [cell addSubview:lblEventsCount];

}

他のセクションにも上と下と同じコードがあるため、完全なコードは掲載していません。

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
         if(tableView == self.mWeekTable)
  {
      int evntCnt;
      if(indexPath.section == 0)
      {
           evntCnt = [self.mArrDayOneEvents count];
          if(indexPath.row == 0)
          {
              isWeekListOne  = !isWeekListOne;

              if(isWeekListOne)
              {
                  if(evntCnt == 0)
                  {
                      myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_blank.png"]];
                  }
                  else{
                      myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_bg-.png"]];
                  }

              }
              else{
                  if(evntCnt == 0)
                  {
                      myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_blank.png"]];
                  }
                  else{
                      myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_bg+.png"]];
                  }
              }
              selectedCellIndexPath = indexPath.row;
              [self.mWeekTable reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
          }
      }
}

int evntCnt は、イベントのあるセクションをチェックするために使用されます。不明な点があればお尋ねください。上記でヘルプを探しています。前もって感謝します。

4

1 に答える 1

0

あなたの問題はセルの再利用に関連しているようです。一般的に、次の 2 つの点に注意してください。

  1. すべての異なる構成が異なるセル識別子 (異なる値) を使用していることを確認してください。
  2. セルを再利用するときは、すべてをリセットします

現在、セルに多くのサブビューを追加しています。これらは、セルが再利用されるときに削除されません。そのため、画像はそこにとどまり、本来あるべきでない行に表示されます。最善の選択肢は、カスタム セル サブクラスを使用して、コードにサブビューの作成がすべて含まれないようにし、適切なサブビューを表示および非表示にするセルのモードを設定する簡単な方法を追加することです。次に、セルを作成/再利用するときに常にモードを設定します。

于 2013-06-27T22:56:12.160 に答える