我有一个表格视图,其中每个单元格都有一个UISwitch
,当我选择一个单元格时,我将能够切换UISwitch
,当我在表格视图中选择下一个单元格时,我不应该能够切换它,或者即使我想切换它之前选择应该用 , 关闭切换UIAlertView
,所以基本上UITableViewCell
应该只允许切换一个。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"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];
}
}