2013-02-17 77 views
2

我有一个UITableView应该只显示一个UISwitch在第一部分,第一行,但由于某种原因,开关正在显示在第4部分。另外,当开关打开/关闭时,一个新的UISwitch出现在另一个单元中。它表现得很奇怪。任何帮助将不胜感激。iOS UITableView绘制的单元格奇怪

代码:

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

    PrettyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
     cell.tableViewBackgroundColor = tableView.backgroundColor; 
    } 

    [cell prepareForTableView:tableView indexPath:indexPath]; 
    cell.cornerRadius = 10; 

    if (indexPath.section == 0) { 
     if (indexPath.row == 0) { 
      cell.textLabel.text = @"Push Notifications"; 
      pushNotificationSwitch = [[UISwitch alloc] init]; 
      pushNotificationSwitch.on = pushSwitchState; 
      cell.accessoryView = pushNotificationSwitch; 
      [pushNotificationSwitch addTarget:self action:@selector(pushSwitch:) forControlEvents:UIControlEventValueChanged]; 
      cell.userInteractionEnabled = YES; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 
    } 

    if (indexPath.section == 1) { 
     if (indexPath.row == 0) { 
      cell.textLabel.text = @"Department Based"; 
      if (departmentBased) { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      else { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      } 
      cell.userInteractionEnabled = YES; 
     } 
     if (indexPath.row == 1) { 
      cell.textLabel.text = @"Location Based (GPS)"; 
      if (locationBased) { 
       cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      } 
      else { 
       cell.accessoryType = UITableViewCellAccessoryNone; 
      }   cell.userInteractionEnabled = YES; 
     } 
    } 

    if (departmentBased) { 
     if (indexPath.section == 2) { 
      if (indexPath.row == 0) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 1) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 2) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 3) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 4) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 5) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 6) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
     } 
     if (indexPath.section == 3) { 
      if (indexPath.row == 0) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 1) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 2) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 3) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 4) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 5) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 6) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 7) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
      if (indexPath.row == 8) { 
       cell.textLabel.text = @"xxxxxx"; 
      } 
     } 
    } 
    return cell; 
} 
+0

请学会使用'if'块的'else'部分。或者使用'switch'语句。更好的是,为您的数据设置一系列标签数组,以便消除大部分代码。 – rmaddy 2013-02-17 05:45:39

回答

1

遇到电池可重用的iOS中的作用。您为所有单元格使用相同的单元格标识符,因此当您滚动时,旧单元格将变为可用并从tableView中出列。

尝试后设置cell.accessoryView到零右:

if (cell == nil) { 
    cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    cell.tableViewBackgroundColor = tableView.backgroundColor; 
} 

cell.accessoryView = nil; 

另外,选择不同的小区标识为第一indexPath,确保你出列时考虑到这一点。

BOOL firstCell = indexPath.row == 0 && indexPath.section == 0; 
NSString *CellIdentifier = firstCell ? @"CellWithSwitch" : @"Cell"; 

PrettyTableViewCell *cell = 
     [tableViewdequeueReusableCellWithIdentifier: CellIdentifier]; 

if (cell == nil) { 
    cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    cell.tableViewBackgroundColor = tableView.backgroundColor; 
} 
+0

太棒了,工作。我仍然试图了解tableView在绘制单元格时的工作方式。 – 2013-02-17 05:48:07

+0

很高兴帮助!当你有机会时,请不要忘记标记为答案。 – abellina 2013-02-17 06:05:49