简体   繁体   中英

Only one UISwitch should be able to toggle in UITableviewCell

I have a tableview where each cell has a UISwitch , when i select a cell i will be able to toggle UISwitch ,when i select the next cell in tableview i shouldn't be able to toggle it or even if i want to toggle it the earlier selected toggle should get switched off with a UIAlertView ,,so basically only one UITableViewCell should be allowed to toggle.

- (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];

                }
            }

        }

    }

}

Here is the code that deals with the 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];
} 
}

3 steps-

  1. set setUserInteractionEnabled to NO of all the UISwitch in tableView:cellForRowAtIndexPath:

  2. set setUSerIntertactionEnabled to YES to the UISwitch of particular cell in tableView:didSelectRowAtIndexPath

  3. set setUSerIntertactionEnabled to NO to the UISwitch of particular cell in tableView:didDeselectRowAtIndexPath

That's all you need to do.

Edited:

for 2 and 3 you have to take the reference of the cell that you are selecting or deselecting.

For that you have to do the following in both 2 and 3

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

I had a similar simulation with switches.I made a customview with UiSwitch assigned tags and by default disabled the user interaction.This view was set as accessoryview of the cells in the method cellForRowAtIndexPath.

To enable interaction of the active cell implement

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

According to NSIndexpath find in yourtableview cell the customview with switch and set its userinteractionenabled property yo YES

For better user experience you should use notifyMe.enabled rather than notifyMe.userInteractionEnabled .

At first, disable all the switches. You could achieve this in cellForRowAtIndexPath:

…
cell.notifyMe.enabled = NO;
…

To disable the switch in cell that is being deselected:

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

And to enable the switch in cell that will soon be selected:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM