【问题标题】:Only one UISwitch should be able to toggle in UITableviewCell只有一个 UISwitch 应该能够在 UITableviewCell 中切换
【发布时间】:2012-12-10 08:35:32
【问题描述】:

我有一个表格视图,其中每个单元格都有一个 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];
} 
}

【问题讨论】:

  • 你有什么问题?您包含了一些表格代码,但其中没有包含 UISwitches ——您可能会更幸运地获得特定问题的答案以及您已经在代码中尝试过的东西。
  • 我想弄清楚如何将 uiswitches 添加到 tableviewcell 中

标签: iphone objective-c xcode uitableview uiswitch


【解决方案1】:

3 步-

  1. tableView:cellForRowAtIndexPath: 中的所有UISwitch 中将setUserInteractionEnabled 设置为NO

  2. setUSerIntertactionEnabled 设置为YEStableView:didSelectRowAtIndexPath 中特定单元格的UISwitch

  3. setUSerIntertactionEnabled 设置为NOtableView:didDeselectRowAtIndexPath 中特定单元格的UISwitch

这就是你需要做的。

已编辑:

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

为此,您必须在 2 和 3 中执行以下操作

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

【讨论】:

  • notifyMe 是 UISwitch 的对象。 [notifyMe setUserInteractionEnabled:YES];我做得对吗?
【解决方案2】:

我对开关进行了类似的模拟。我使用 UiSwitch 分配的标签创建了一个自定义视图,默认情况下禁用了用户交互。此视图在方法 cellForRowAtIndexPath 中设置为单元格的附件视图。

启用活动单元实现的交互

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

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多