Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UItableViewCells中的单选按钮逻辑_Ios_Objective C_Uitableview - Fatal编程技术网

Ios UItableViewCells中的单选按钮逻辑

Ios UItableViewCells中的单选按钮逻辑,ios,objective-c,uitableview,Ios,Objective C,Uitableview,嘿,我在一个屏幕上工作,在这个屏幕上,用户有选项组,例如“饮料”,这是我表格视图中部分的标题,他们的选择是“7up”、“coke”,等等,这是我表格的单元格 现在,每个选项组选项(单词顺序中的每个单元格)都有一个单选按钮。我想实现这一点。我面临的问题是,若用户选择任何单元格的单选按钮,那个么应该取消选择其他单选按钮,但如何取消选择 请提供任何帮助一个解决方案是使用表视图本机选择功能 在标准UITableView中,一次只能选择一行,您可以利用这一点。通过将情节提要中的“选择”设置为“无”,行的选

嘿,我在一个屏幕上工作,在这个屏幕上,用户有选项组,例如“饮料”,这是我表格视图中部分的标题,他们的选择是“7up”、“coke”,等等,这是我表格的单元格

现在,每个选项组选项(单词顺序中的每个单元格)都有一个单选按钮。我想实现这一点。我面临的问题是,若用户选择任何单元格的单选按钮,那个么应该取消选择其他单选按钮,但如何取消选择


请提供任何帮助一个解决方案是使用表视图本机选择功能

在标准UITableView中,一次只能选择一行,您可以利用这一点。通过将情节提要中的“选择”设置为“无”,行的选择将不可见

现在,您可以实现自己的选择显示。您可以重写方法
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
,以在选定单元格时更新单元格

您还可以重写方法
-(void)tableView:(UITableView*)tableView diddescrowatindexpath:(nsindepath*)indepath
,以便在不再选择单元格时更改单元格

UITableViewDelegate在进行新选择时,会自动调用旧选择的
didSelectRowAtIndexPath
,使选择像单选按钮一样保持唯一

我为你准备了一个小样本项目,你可以试试

希望我至少帮了点忙


干杯

您应该创建一个函数来检查自定义单元格中的单选按钮,并实现一个委托方法来通知TableViewController该单元格上的按钮已被选中

TableViewController需要实现该委托(不要忘记设置每个单元格。delegate=self)

然后在委托方法中创建一个循环,以取消选中该部分中除刚才选中的单元格外的所有单元格单选按钮

诸如此类:

这是一个带有按钮的自定义UITableViewCell。 选中和取消选中的图像需要看起来像选中和取消选中的单选按钮 以下是.h文件:

//RadioCell.h
@protocol RadioCellDelegate <NSObject>
    -(void) myRadioCellDelegateDidCheckRadioButton:(RadioCell*)checkedCell;
@end

@interface RadioCell : UITableViewCell
     -(void) unCheckRadio;
     @property (nonatomic, weak) id <RadioCellDelegate> delegate;
@end
现在只需使用RadioCell(在.m文件中)调整tableview控制器

//MyTableViewController.m
@接口MyTableViewController()
@结束
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“无线蜂窝”;
RadioCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[RadioCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textlab=@“Coke”;//或任何您想要的内容
cell.delegate=self;
返回单元;
}
-(无效)MyRadioCellDelegatedDidCheckRadioButton:(RadioCell*)checkedCell{
nsindepath*checkPath=[self.tableView indexPathForCell:checkedCell];
对于(int section=0;section<[self.tableView numberOfSections];section++){
if(section==checkPath.section){
对于(int row=0;row<[self.tableView numberofrowsinssection:section];row++){
nsindepath*cellPath=[nsindepath indexPathForRow:row-instation:section];
RadioCell*cell=(CustomCell*)[tableView cellForRowAtIndexPath:cellPath];
if(checkPath.row!=cellPath.row){
[细胞不受约束];
}
}
}     
}
}

两个选项单选按钮UITableView的简单解决方案(但您已经想到了):


你的实际问题是什么?取消选择其他单元格?但我没有使用自定义单元格。我的单元格代码可以在CellForRowatineXpath中找到。方法:(它可以用我当前在CellForRowatineXpath中的代码进行调整吗?)如果有机会,我会很感激你如何在你的单元格中添加你的单选按钮?我刚刚编辑了代码,这样一切都会正常的。如果你有任何问题,请不要犹豫。
//RadioCell.m
@property (nonatomic, assign) UIButton myRadio;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
   _myRadio = [UIButton buttonWithType:UIButtonTypeCustom];
   [_myRadio setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal];
   [_myRadio setImage:[UIImage imageNamed:@"check"] UIControlStateSelected];
   [_myRadio addTarget:self action:@selector(radioTouched)orControlEvents:UIControlEventTouchUpInside];
   _myRadio.isSelected = NO;

   //don't forget to set _myRadio frame
   [self addSubview:_myRadio];
}

-(void) checkRadio {
   _myradio.isSelected = YES;
}

-(void) unCheckRadio {
   _myradio.isSelected = NO;
}

-(void) radioTouched {
     if(_myradio.isSelected == YES) {
          return;
     } 
     else {
       [self checkRadio]
       [_delegate myRadioCellDelegateDidCheckRadioButton:self];
     }
}
//MyTableViewController.m

@interface MyTableViewController () <RadioCellDelegate>
@end

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
   {
      static NSString *CellIdentifier = @"RadioCell";
      RadioCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
      if (cell == nil) {
          cell = [[RadioCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
      }

      cell.textLabel = @"Coke"; //or whatever you want
      cell.delegate = self;

      return cell;
   }

-(void) myRadioCellDelegateDidCheckRadioButton:(RadioCell*)checkedCell {
     NSIndexPath *checkPath = [self.tableView indexPathForCell:checkedCell];

     for (int section = 0; section < [self.tableView numberOfSections]; section++) {
         if(section == checkPath.section) {
             for (int row = 0; row < [self.tableView numberOfRowsInSection:section]; row++) {
                   NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
                   RadioCell* cell = (CustomCell*)[tableView cellForRowAtIndexPath:cellPath];

                   if(checkPath.row != cellPath.row) {
                       [cell unCheckRadio];
                   }
             }
         }     
     }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSIndexPath *newIP;

    if (!indexPath.row)
        newIP = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
    else
        newIP = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
        cell.accessoryType = UITableViewCellAccessoryNone;
    else{
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:newIP];
        newCell.accessoryType = UITableViewCellAccessoryNone;
    }
}