2014-12-23 57 views
2

我有一個包含uiswitch(obj-c)的uitableviewcell。該開關工作正常,但是當點擊tableview外的按鈕時,我需要單元格中的uiswitch重新繪製並禁用它。我正在嘗試以下,但它不會工作?我的CellForRowAtIndex被調用,我的代碼塊被稱爲禁用UISwitch,但沒有任何反應?帶有UISwitch的UITableViewCell不會重繪?

- (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; 
    } 
+0

沒關係那些其他評論(刪除)...我看到你在哪裏添加開關作爲子視圖。但是您應該(1)重用這些開關並(2)在本地聲明它們。 –

+0

你想在什麼情況下關閉開關?你沒有在'cellForRowAtIndexPath'中說明。我開始寫出答案,但有太多未知數。你需要發佈更多的代碼。特別是從thingSwitchChanged: –

回答

0

我通過繼承UITableViewCell並使用委託實現了開關單元的成功。

@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 

希望它給你一些方向。

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:發佈代碼。

0

您目前無法做到這一點。你沒有使用自我獲得單元格內的開關。 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; 
} 
+0

但是像你一樣內聯這樣做,我怎樣才能訪問其他代碼中的開關,因爲我沒有參考了? – jdog

+0

@jdog:你爲什麼用其他方法訪問它,以及你如何訪問它? –

相關問題