2013-03-18 85 views
0

我目前正在开发一个UITableView,其中带有复选标记的选项。 问题是我需要对一次选择多少项目进行限制。复选标记单选

例如:

NO洋葱,NO盐,YES牛奶,NO桔子。

如本例所示,我需要一个限制,以便用户只能选择一个项目。

目前代码:

-(void)checkButtonPressed:(id)sender event:(id)event { 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.myTableView]; 
    indexP = [self.myTableView indexPathForRowAtPoint: currentTouchPosition]; 

    NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; 
    //MasterCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MasterCell"]; 
    item = [arrayData objectAtIndex:indexP.row]; 

    if (indexP != nil) { 
     [self tableView: self.myTableView accessoryButtonTappedForRowWithIndexPath:indexP]; 
    } 

    [self.myTableView reloadData]; 
} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath { 
    NSMutableDictionary *item = [arrayData objectAtIndex:indexPath.row]; 

    BOOL checked = [[item objectForKey:@"checked"] boolValue]; 

    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"]; 

    UITableViewCell *cell = [item objectForKey:@"cell"]; 
    UIButton *button = (UIButton *)cell.accessoryView; 

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"]; 
    [button setBackgroundImage:newImage forState:UIControlStateNormal]; 

    NSLog(@"accessoryButtonTappedForRowWithIndexPath____"); 
} 

我的cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSMutableDictionary *item = [arrayData objectAtIndex:indexPath.row]; 
    UILabel *nomeLabel = (UILabel *)[cell viewWithTag:2]; 

    UIButton *btnCheck = (UIButton *)[cell viewWithTag:3]; 
    BOOL checked = [[item objectForKey:@"checked"] boolValue]; 
    UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"]; 
    [btnCheck setBackgroundImage:image forState:UIControlStateNormal]; 

    nomeLabel.text = [item valueForKey:@"nome"]; 
    nomeLabel.textColor = [UIColor blackColor]; 
    [btnCheck addTarget:self action:@selector(checkButtonPressed:event:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

在此先感谢和抱歉,我的英语不好,请不要往下投票,只问如果您有任何疑问,我总是在附近回答。

+1

你需要什么?单选或多选表列表项? – 2013-03-18 10:19:36

+0

单独选择:) – Bruno 2013-03-18 10:34:15

+0

我已经与选中未选中的图片共享自定义单元格示例 – 2013-03-18 10:45:11

回答

2

试试这个::

.h文件中,

@property(nonatomic, retain) NSIndexPath *lastIndexPath; 

.m文件,

int oldRow, newRow; 

表方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CustomCellIdentifier = @"customCell"; 
    customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib; 
     nib= [[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil]; 
     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[customCell class]]) 
       cell = (customCell *)oneObject; 

     newRow = [indexPath row]; 
     oldRow = [[self lastIndexPath] row]; 


     cell.accessibilityViewIsModal = (newRow == oldRow && lastIndexPath != nil) ? NO : YES; 

     if(cell.accessibilityViewIsModal == NO) 
      cell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"]; 
     else 
      cell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Unchecked_New.png"]; 

     return cell; 
    } 
} 

DidSelect ::

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    newRow = [indexPath row]; 
    oldRow = [[self lastIndexPath] row]; 

    //Check Mark Feature 
    if (newRow != oldRow) 
    { 
     customCell *newCell = (customCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     newCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"]; 

     customCell *oldCell = (customCell *)[tableView cellForRowAtIndexPath:lastIndexPath]; 
     oldCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Unchecked_New.png"]; 

     [self setLastIndexPath:indexPath]; 
    } 
    else 
    { 
     customCell *newCell = (customCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     newCell.imgCheck.image = [UIImage imageNamed:@"Checkbox_Checked_New.png"]; 

     [self setLastIndexPath:indexPath]; 
    } 
} 
+0

我已经在这里拍摄了带有imageview的自定义单元格,并在其上设置了选中和未选中的图像。 – 2013-03-18 10:44:35

1

基本上,对于这种业务逻辑,你将不得不自己编写代码。当使用表格视图作为基本上单选按钮时,强制执行单一选择要求的唯一方法是编写处理用户情况的代码,试图按照系统要求的方式选择第二项:

  • 允许多个选择(这里不是这种情况,但在某些情况下,您可能会)
  • 删除所有与此不兼容的其他检查,检查所选项目并告诉表视图以刷新所有行已更改
  • 以某种方式向用户呈现错误(眨眼,提示,声音) - 当您允许直接操作时,几乎从不是一个好主意。
1

尝试这样可能会帮助你,这里lastIndexPath是在.h文件中声明的NSIndexPath变量。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     UITableViewCell* Cell = [tableView cellForRowAtIndexPath:indexPath]; 
     int new = [indexPath row]; 
     int old = (lastIndexPath != nil) ? [lastIndexPath row] : -1; 

     if(new != old) 
     { 
      Cell.accessoryType = UITableViewCellAccessoryCheckmark; 
      UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath]; 
      oldCell.accessoryType = UITableViewCellAccessoryNone; 
      lastIndexPath = indexPath; 

     } 

    }