2013-03-25 26 views
0

我有一個表格視圖,每個單元格中有一個開關。我想隱藏編輯模式下的開關。我用下面的代碼完成無法訪問uitableview中的不可見單元格

-(void)displaySwitch:(BOOL)status { 

    int count = [self.tblView numberOfRowsInSection:0]; 
    int i; 
    for (i = 0; i<count; i++) { 
     UITableViewCell *eachCell = [self.tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     //NSLog(@"%@",eachCell); 
     NSArray *subViews = [eachCell.contentView subviews]; 
     for (UISwitch *eachObject in subViews) { 
      if ([eachObject isKindOfClass:[UISwitch class]]) { 

       CATransition *animation = [CATransition animation]; 
       animation.type = kCATransitionFade; 
       animation.subtype = kCATransitionFromLeft; 
       animation.duration = 1.4; 
       animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
       [eachObject.layer addAnimation:animation forKey:kCATransition]; 
       eachObject.hidden = status; 

      } 

     } 

    } 

} 

它適用於所有可見的單元格。但我不能使用此代碼訪問任何不可見的單元格。單元返回零。所以不可見單元格中的開關不會隱藏。我怎樣才能克服這個問題。請幫助我

還有一件事,動畫不在這裏工作。但這是次要問題。

+0

,因爲他們尚未創建 – 2013-03-25 09:23:07

+0

您需要將您的代碼cellForRowAtIndexPath()來實現你的需求。 – 2013-03-25 09:25:32

回答

1

修改你的代碼只看看

-(void)displaySwitch:(BOOL)status onCell:(UITableViewCell*)cell { 

    NSArray *subViews = [cell.contentView subviews]; 
    for (UISwitch *eachObject in subViews) { 
     if ([eachObject isKindOfClass:[UISwitch class]]) { 

      CATransition *animation = [CATransition animation]; 
      animation.type = kCATransitionFade; 
      animation.subtype = kCATransitionFromLeft; 
      animation.duration = 1.4; 
      animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
      [eachObject.layer addAnimation:animation forKey:kCATransition]; 
      eachObject.hidden = status; 

     } 
    } 
} 

然後調用這個函數

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(cell == nil){ 

     //Cell initialization 

    } 

    [self displaySwitch:self.editing onCell:cell]; 
} 
的,因爲你不能訪問他們
+1

謝謝。你真了不起。它拯救了我的生命 – manujmv 2013-03-25 10:01:15

相關問題