0

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

例如:

没有洋葱,没有盐,没有牛奶,没有橙子。

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

当前代码:

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

提前感谢并为我糟糕的英语感到抱歉,请不要投反对票,只要问您是否有任何疑问,我都会随时回答。

4

3 回答 3

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;
    }
}

选择::

- (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];
    }
}
于 2013-03-18T10:43:29.457 回答
1

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

  • 允许多项选择(这里不是这种情况,但在某些情况下您可能会这样做)
  • 移除与此不兼容的所有其他检查,检查所选项目,并告诉表格视图刷新您刚刚更改的所有行
  • 以某种方式(眨眼、警告、声音)向用户显示错误——当您可以允许直接操作时,这几乎不是一个好主意。
于 2013-03-18T10:27:29.417 回答
1

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

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

        } 

    }   
于 2013-03-18T10:38:37.657 回答