2013-04-25 59 views
0

我在我的表格视图中有一个非常奇怪的问题。
我正在使用不同的表格视图部分,每个部分有不同的行数。我在表格视图的单元格中有一些文本字段。问题是,当我向下滚动表格视图时,第一部分文本字段的第一个单元格和最后一部分第一行文本字段彼此重叠。当我向上滚动表格视图时会发生同样的事情。UITableView部分条件不起作用

static NSString *cellIdetifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier]; 
    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdetifier]; 
    } 
    cell.detailTextLabel.textColor = [UIColor blackColor]; 

    if(indexPath.section == 0) 
    { 
     if(indexPath.row == 0) 
     { 
      titleAdded = 1; 
      self.nameView.backgroundColor = [UIColor clearColor]; 
      self.eventName.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
      [cell addSubview:self.nameView]; 
     } 
     else if (indexPath.row == 1) 
     { 
      NSLog(@"in section 0"); 
      self.locationView.backgroundColor = [UIColor clearColor]; 
      self.locationField.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
      [cell addSubview:self.locationView]; 
     } 
    } 
    else if (indexPath.section == 1) 
    { 
     if(indexPath.row == 0) 
     { 
      cell.textLabel.text = NSLocalizedString(@"Starts", @""); 
      if(self.selectedDate != nil) 
      { 
       NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
       //[formatter setDateFormat:@"yyyy-mm-dd"]; 
       [formatter setDateStyle:NSDateFormatterFullStyle]; 

       NSString *stringFromDate = [formatter stringFromDate:self.selectedDate]; 

       cell.detailTextLabel.text = stringFromDate; 
      } 
     } 
     else if(indexPath.row == 1) 
     { 
      cell.textLabel.text = NSLocalizedString(@"Ends", @""); 
      if(self.selectedDate != nil) 
      { 
       NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
       //[formatter setDateFormat:@"yyyy-mm-dd"]; 
       [formatter setTimeStyle:NSDateFormatterShortStyle]; 

       NSString *stringFromDate = [formatter stringFromDate:self.selectedTime]; 

       cell.detailTextLabel.text = stringFromDate; 
      } 
     } 

    } 
    else if (indexPath.section == 2) 
    { 
     if(indexPath.row == 0) 
     { 
      cell.textLabel.text = NSLocalizedString(@"Repeat",@""); 
      cell.detailTextLabel.text = NSLocalizedString(@"Never", @""); 
     } 
    } 
    else if (indexPath.section == 3) 
    { 
     if(indexPath.row == 0) 
     { 
      cell.textLabel.text = NSLocalizedString(@"Alert", @""); 
      if(![[[NSUserDefaults standardUserDefaults] valueForKey:@"ringtone"] isEqualToString:@""] && [[NSUserDefaults standardUserDefaults] valueForKey:@"ringtone"] != nil) 
      { 
       cell.detailTextLabel.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"ringtone"]; 
      } 
      else 
      { 
       cell.detailTextLabel.text = NSLocalizedString(@"None", @""); 
      } 
     } 
    } 
    else if (indexPath.section == 4) 
    { 
     if(indexPath.row == 0) 
     { 
      cell.textLabel.text = NSLocalizedString(@"Calendar", @""); 
      cell.detailTextLabel.text = NSLocalizedString(@"Home", @""); 
     } 
    } 
    else if(indexPath.section == 5 && indexPath.section != 0) 
    { 
     if(indexPath.row == 0) 
     { 
      self.urlView.backgroundColor = [UIColor clearColor]; 
      self.urlField.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0]; 
      [cell addSubview:self.urlView]; 
     } 
    } 

这是我的方法cellforRowatIndexPath
任何变通方法的代码。我已经调试过它,当它出现在第五节if语句时,它找不到节的值,并跳转到if语句中。然后出现问题。 我也检查过,如果它是零或不是没用。

+1

你知道'indexPath.section = 5'是索引编号从0开始的第6个部分。 – Kal 2013-04-25 16:37:25

+0

请显示出错的屏幕截图。谢谢! – matt 2013-04-25 16:37:35

+2

'else if(indexPath.section == 5 && indexPath.section!= 0)'* *应该表示什么* – matt 2013-04-25 16:37:52

回答

5

问题是,您正在为每种类型的单元格使用相同的重用单元格(或同一批次的重用单元格),并在每次重新使用时添加子视图而不删除它们。您应该为每种类型的单元格使用不同的单元格标识符,并且只能在初始化中添加子视图。

编辑:另外,你应该添加子视图到单元格的contentView,而不是直接。

你到处放[cell addSubview:whatever],取代基于此答案由亚光第一评论[cell.contentView addSubview:whatever]

,这里是实现这个的一个快速和肮脏的方式:

为了消除所有的子视图电池:

for (UIView *view in cell.contentView.subviews) 
{ 
    [view removeFromSuperview]; 
} 

把那个正确的这行代码中的前

cell.detailTextLabel.textColor = [UIColor blackColor]; 

请记住,该解决方案在具有许多单元格的表格中不会很好。你应该真的把每个单元格分成它自己的类型,也许你可以使用它们的tag属性跟踪任何自定义子视图。

+1

或者在每次添加适合本节/行的子视图之前删除所有子视图。但我更喜欢你的解决方案,迪马;这是很好的使用多个标识符。 – matt 2013-04-25 16:39:03

+0

确实可以更简单地进行编码,但如果有很多行,则可能会出现性能问题。 – Dima 2013-04-25 16:40:09

+0

如何在添加其他人之前删除所有子视图? – 2013-04-25 16:48:09