【问题标题】:UITableView another cell image also changes when selectedUITableView 另一个单元格图像在选择时也会发生变化
【发布时间】:2013-06-27 22:35:20
【问题描述】:

我有一个 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 用于检查那些有事件的部分。如果有什么不清楚的请询问。在上面寻求帮助。提前致谢。

【问题讨论】:

  • 为什么你总是创建一个新单元格,即使你设法让一个单元格出列?
  • 我猜你所有的单元格标识符都是同一个字符串,有时你使用出队的单元格,有时你创建一个新的单元格?
  • @Wain 发现了几个错误并已删除,但问题未解决。

标签: iphone ios uitableview


【解决方案1】:

您的问题似乎与细胞重复使用有关。一般来说,有两件事要看:

  1. 确保每个不同的配置使用不同的小区标识符(具有不同的值)
  2. 重复使用单元格时,重置所有内容

目前,您正在向单元格添加大量子视图。当单元被重复使用时,这些不会被删除。因此,图像会保留在那里并显示在它们不应该出现的行上。您最好的选择是使用自定义单元格子类,这样您的代码就不会创建所有子视图,并添加一种简单的方法来设置显示和隐藏适当子视图的单元格模式。然后在创建/重用单元格时始终设置模式。

【讨论】:

    猜你喜欢
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2012-10-18
    相关资源
    最近更新 更多