带有UISwitch的UITableViewCell不会重绘?

时间:2014-12-23 18:00:55

标签: ios objective-c uitableview uiswitch

我有一个包含uiswitch(obj-c)的uitableviewcell。开关工作正常,但是当单击tableview外的按钮时,我需要单元格中的uiswitch重绘并禁用它。我正在尝试以下方法,但它不会起作用?我的CellForRowAtIndex被调用,我的代码块被调用,表示禁用UISwitch但没有任何反应?

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

        NSString *danID = [[NSUserDefaults standardUserDefaults]objectForKey:kSavingdanID];


        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier];
            cell.backgroundColor = [UIColor clearColor];
            cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
            cell.selectedBackgroundView = [[UIView alloc] init];
        }


        if(indexPath.row == 0) { // Mute thing Calls

            cell.textLabel.text = @"Public Calls";
            self.thingSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 13, 0, 0)];
            [cell.contentView addSubview:self.thingSwitchView];
    //        cell.accessoryView = switchView;
            [self.thingSwitchView setOn:YES animated:NO];
            [self.thingSwitchView addTarget:self action:@selector(thingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
            cell.backgroundColor = [UIColor clearColor];
            cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];

        } else if(indexPath.row == 1) { // Mute boo Calls

            cell.textLabel.text = @"boo Calls";
            self.booSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 12, 0, 0)];
            [cell.contentView addSubview:self.booSwitchView];
    //        cell.accessoryView = switchView;
            [self.booSwitchView setOn:YES animated:NO];
            [self.booSwitchView addTarget:self action:@selector(booSwitchChanged:) forControlEvents:UIControlEventValueChanged];
            cell.backgroundColor = [UIColor clearColor];
            cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];

        } else {
            // do nothing
        }


        if([NSString isEmpty:danID]){
            [self.thingSwitchView setEnabled:NO];
            [self.booSwitchView setEnabled:NO];
            [cell.backgroundView setNeedsDisplay];
            [cell setNeedsDisplay];
        }

        return cell;
    }

3 个答案:

答案 0 :(得分:0)

通过继承UITableViewCell并使用委托,我成功实现了switch cell。

@class EGSwitchCell;

@protocol EGSwitchCellDelegate <NSObject>

-(void)switchCellDidChange:(EGSwitchCell *)switchCell;

@end

@interface EGSwitchCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UISwitch *mySwitch;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;

@property (nonatomic, weak) id<EGSwitchCellDelegate> delegate;

-(IBAction)switchChanged:(id)sender;

@end

希望它能给你一些指导。

答案 1 :(得分:0)

首先,在每次调用cellForRowAtIndexPath:期间添加新的切换作为子视图,您将在旧的切换之上堆积新的切换。我建议您在创建时将开关添加到相应的单元格中。同样,我还建议您在创建单元格时设置单元格的contentView.backgroundColor,因为它不会发生变化。

我花了一些时间,但我想我知道为什么你要制作你的开关类变量......我假设你的表中只有一个部分(?)和所以这些开关只出现一次。如果是这种情况,我们可以使用这些类变量,因为即使交换机发生变化,它们也可以保持变量。

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

    NSString *danID = [[NSUserDefaults standardUserDefaults]objectForKey:kSavingdanID];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
        cell.selectedBackgroundView = [[UIView alloc] init];

        if (indexPath.row == 0) {
            self.thingSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 13, 0, 0)];
            [self.thingSwitchView setOn:YES animated:NO];
            [self.thingSwitchView addTarget:self action:@selector(thingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
            [cell.contentView addSubview:self.thingSwitchView];

            cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];

        } else if (indexPath.row == 1) {
            self.booSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 12, 0, 0)];
            [self.booSwitchView setOn:YES animated:NO];
            [self.booSwitchView addTarget:self action:@selector(booSwitchChanged:) forControlEvents:UIControlEventValueChanged];
            [cell.contentView addSubview:self.booSwitchView];

            cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
        }
    }

    if (indexPath.row == 0) { // Mute thing Calls

        cell.textLabel.text = @"Public Calls";

    } else if (indexPath.row == 1) { // Mute boo Calls

        cell.textLabel.text = @"boo Calls";
    }

    if ([NSString isEmpty:danID]) {
        [self.thingSwitchView setEnabled:NO];
        [self.booSwitchView setEnabled:NO];
    } else {
        [self.thingSwitchView setEnabled:YES];
        [self.booSwitchView setEnabled:YES];
    }

    return cell;
}

另外,请务必按照您的意图更改NSUserDefaults objectForKey kSavingdanID。如果您仍然遇到问题,我建议您从thingSwitchChanged:booSwitchChanged:发布您的代码。

答案 2 :(得分:0)

你不能以现在的方式做到这一点。您没有使用self获取单元格内的开关。 Self.thingSwitchViewself.booSwitchView包含最后分配的交换机。它不会是单元内的开关。即使重复使用该单元,您每次都会创建一个新的开关。这也不是一个好习惯。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  thingic NSString *cellIdentifier = @"Cell";
  NSString *danID = [[NSUserDefaults standardUserDefaults]objectForKey:kSavingdanID];


  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  cell.selectionStyle   = UITableViewCellSelectionStyleNone;
  if (cell == nil)
  {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:cellIdentifier];
      cell.backgroundColor = [UIColor clearColor];
      cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:21];
      cell.textLabel.textColor = [UIColor whiteColor];
      cell.textLabel.highlightedTextColor = [UIColor lightGrayColor];
      cell.selectedBackgroundView = [[UIView alloc] init];
  }

  if(indexPath.row == 0 && ![cell.contentView viewWithTag:101])
  { // Mute thing Calls
       cell.textLabel.text = @"Public Calls";
       self.thingSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 13, 0, 0)];
       self.thingSwitchView.tag = 101;
       [cell.contentView addSubview:self.thingSwitchView];
       [self.thingSwitchView setOn:YES animated:NO];
       [self.thingSwitchView addTarget:self action:@selector(thingSwitchChanged:) forControlEvents:UIControlEventValueChanged];
       cell.backgroundColor = [UIColor clearColor];
       cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
  }
  else if(indexPath.row == 1 && ![cell.contentView viewWithTag:102])
  { // Mute boo Calls
       cell.textLabel.text = @"boo Calls";
       self.booSwitchView = [[UISwitch alloc] initWithFrame:CGRectMake(170, 12, 0, 0)];
       [cell.contentView addSubview:self.booSwitchView];
       self.booSwitchView.tag = 102;
       [self.booSwitchView setOn:YES animated:NO];
       [self.booSwitchView addTarget:self action:@selector(booSwitchChanged:) forControlEvents:UIControlEventValueChanged];
       cell.backgroundColor = [UIColor clearColor];
       cell.contentView.backgroundColor = [UIColor colorWithRed: 0.0/255.0 green: 0.0/255.0 blue: 0.0/255.0 alpha: 0.4];
   }

   UISwitch *thingSwitch = [cell.contentView viewWithTag:101];
   UISwitch *booSwitch   = [cell.contentView viewWithTag:102];
   if([NSString isEmpty:danID])
   {
        [thingSwitch setEnabled:NO];
        [booSwitch setEnabled:NO];
   }
   else
   {
      [thingSwitch setEnabled:YES];
      [booSwitch setEnabled:YES];
   }
   return cell;
}