只有一个UISwitch应该能够在UITableviewCell中切换

时间:2012-12-10 08:35:32

标签: iphone objective-c xcode uitableview uiswitch

我有一个tableview,其中每个单元格都有UISwitch,当我选择一个单元格时,我将能够切换UISwitch,当我选择tableview中的下一个单元格时,我不应该切换它,或者即使我要切换它,早先选择的切换应该使用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];
} 
}

3 个答案:

答案 0 :(得分:3)

3个步骤 -

  1. setUserInteractionEnabled }中的所有NO的{​​{1}}设为UISwitch

  2. tableView:cellForRowAtIndexPath:设置为setUSerIntertactionEnabledYES

  3. 中特定单元格的UISwitch
  4. tableView:didSelectRowAtIndexPath设置为setUSerIntertactionEnabledNO

  5. 中特定单元格的UISwitch

    这就是你需要做的一切。

    <强>编辑:

    对于2和3,您必须参考您正在选择或取消选择的单元格。

    为此,您必须在2和3

    中执行以下操作
    tableView:didDeselectRowAtIndexPath

答案 1 :(得分:0)

我对switch进行了类似的模拟。我使用UiSwitch分配了标签,并默认禁用了用户交互。此视图设置为方法cellForRowAtIndexPath中单元格的附件视图。

启用活动单元实现的交互

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

根据NSIndexpath在yourtableview单元格中找到带有switch的customview并设置其userinteractionenabled属性为是

答案 2 :(得分:0)

为了获得更好的用户体验,您应该使用notifyMe.enablednotifyMe.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;
    }
}