2012-12-10 40 views
0

我有一个tableview,其中每个单元格有一个UISwitch,当我选择一个单元格时,我将能够切换UISwitch,当我选择tableview中的下一个单元格时,我不应该能够切换它,或者即使我想要切换它,早先选择的切换应该被关掉UIAlertView ,,所以基本上只有一个UITableViewCell应该被允许切换。只有一个UISwitch应该能够在UITableviewCell中切换

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *[email protected]"cellIdentifier"; 
     PPtableCell *cell=(PPtableCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if (cell==nil) 
     { 
      cell=[[PPtableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     } 

     cell.remedyImage.image=[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"RemedyImageDic"]; 
     cell.remedyTextLabel.text=[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"RemedyTxtDic"]; 
     cell.remedyLabel.text=[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"RemedyName"]; 
     cell.remedyID=[[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"RemedyID"]intValue]; 
     cell.frequency = [[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"RemedyFrequency"]intValue]; 
     cell.symptomIDNo =[[[remedyArray objectAtIndex:indexPath.row]objectForKey:@"SymptomID"]intValue]; 

     // cell.textLabel.text = [[notify objectAtIndex:indexPath.row] objectForKey:@"notifyData"]; 


     // Specific Background Image Depending upon the Row 

     if (indexPath.row==0) 
     { 
      cell.backgroundCellImage.image=[UIImage imageNamed:@"top-cell.png"]; 
     } 
     else if (indexPath.row==remedyArray.count-1) 
     { 
      cell.backgroundCellImage.image=[UIImage imageNamed:@"bottom-cell.png"]; 
     } 
     else 
     { 
      cell.backgroundCellImage.image=[UIImage imageNamed:@"middle-cell.png"]; 
     } 


     if ([indexPath isEqual:remedySelectedIndexPath]) 
     { 
      cell.isZoomedIn = YES; 
     } 
     else 
     { 
      cell.isZoomedIn = NO; 
     } 

     return cell; 

    } 

} 



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

if(tableView==remedyTableView) 
    { 

     PPtableCell *cell = nil; 
     cell=(PPtableCell*)[tableView cellForRowAtIndexPath:indexPath]; 
     cell.selectionStyle=UITableViewCellSelectionStyleNone; 
     CGSize maximumLabelSize=CGSizeMake(320.0,100.0); 
     CGSize expectedLabelSize=[cell.remedyTextLabel.text sizeWithFont:cell.remedyTextLabel.font constrainedToSize:maximumLabelSize 
                  lineBreakMode:cell.remedyTextLabel.lineBreakMode]; 

     if (expectedLabelSize.height > 17 || expectedLabelSize.width > 260.0) 
     { 
      if ([indexPath isEqual:remedySelectedIndexPath]) 
      { 
       remedySelectedIndexPath = nil; 
       [remedyTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

      } 
      else 
      { 
       oldSelectedIndexPath=remedySelectedIndexPath; 
       remedySelectedIndexPath=indexPath; 

       if (oldSelectedIndexPath==nil) 
       { 
        [remedyTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:remedySelectedIndexPath] withRowAnimation:UITableViewRowAnimationNone]; 

       } 

       else 
       { 
        [remedyTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:oldSelectedIndexPath,remedySelectedIndexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; 

       } 
      } 

     } 

    } 

} 

这里是与UISwitches交易代码:

(IBAction)notification:(id)sender { 
    if (sender == notifyMe) { 
     if(notifyMe.isOn == YES) { 
      toggle = YES; 
      NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 
      [defaults setBool:notifyMe.on forKey:@"switchValueKey"]; 
      [defaults synchronize]; 
      NSLog(@"Notification is ON"); 
     } 
    } else { 
     toggle = NO; 
    } 
} 

if ([cellDelegate respondsToSelector:@selector(notificationReqd:)]) { 
    [cellDelegate notificationReqd:self]; 
} 
} 
+0

你有什么问题?你包含了一些表格代码,但没有UISwitches在里面 - 你可能有更好的运气得到一个具体问题的答案和你已经在代码中尝试过的东西。 –

+0

我想弄清楚如何将uiswitches添加到tableviewcell中 – orgami

回答

0

我也有类似的仿真switches.I由具有UiSwitch分配标签和默认情况下禁用customview用户interaction.This查看在方法cellForRowAtIndexPath中设置为单元的辅助视图。

为了使活性细胞的相互作用实现

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 

根据NSIndexpath在yourtableview细胞找到开关customview并设置其属性userinteractionenabled哟YES

3

3步骤 -

  1. 设置为setUserInteractionEnabledNOUISwitchtableView:cellForRowAtIndexPath:

  2. 设置setUSerIntertactionEnabledYES特定细胞的UISwitchtableView:didSelectRowAtIndexPath

  3. tableView:didDeselectRowAtIndexPath

这就是所有你需要做的设置setUSerIntertactionEnabledNO特定细胞的UISwitch

被修改:

2和3你必须采取您选择小区或取消选择的参考。

对于您所要做的两个2和3

UITableViewCell *cell=[yourTableView cellForRowAtIndexPath:indexPath]; 
cell.notifyMe setUserInteractionEnable=YES; // set NO for step 3 
+0

notifyMe是UISwitch的对象。 [notifyMe setUserInteractionEnabled:YES];我做得对吗? – orgami

+0

请参阅我编辑的答案 –

+0

我无法更改tableview单元格中UISwitch的状态... – orgami

0

以下为更好的用户体验,你应该使用notifyMe.enabled而 不是notifyMe.userInteractionEnabled

首先,禁用所有开关。你可以在cellForRowAtIndexPath:

… 
cell.notifyMe.enabled = NO; 
… 

实现这一目标要禁用被取消在电池开关:

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == tableViewThatDealsWithSwitches) 
    { 
     PPtableCell *cell = (PPTableCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     cell.notifyMe.enabled = NO; 
    } 
    return indexPath; 
} 

并使很快就会选择在细胞开关:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (tableView == tableViewThatDealsWithSwitches) 
    { 
     cell.notifyMe.enabled = YES; 
     return indexPath; 
    } 
}