-1

当用户从表视图中取消选择该值时,我想在用户从表视图中选择行时设置按钮标题也从标题名称中删除。

这是我的代码。我只想选择 2 行。

        UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];


    if ([thisCell accessoryType] == UITableViewCellAccessoryNone) 
    {
        if(i<2)
        {
            //thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
            [thisCell setAccessoryType:UITableViewCellAccessoryCheckmark];
            [selectedIndexes addObject:[catarray objectAtIndex:indexPath.row]];
            [selectedIndexesTag addObject:[idtag objectAtIndex:indexPath.row]];
            [btncat setTitle:[catarray objectAtIndex:indexPath.row]     
            forState:UIControlStateNormal];
            i++;
        }
        else
        {
            UIAlertView *alert= [[UIAlertView alloc]initWithTitle:@"Error"   
            message:@"User Can only Select two values" delegate:self 
            cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];

            [alert show];
        }

    }
    else
    {
        //thisCell.accessoryType = UITableViewCellAccessoryNone;
        [thisCell setAccessoryType:UITableViewCellAccessoryNone];
        [selectedIndexes removeObject:[catarray objectAtIndex:indexPath.row]];
        [selectedIndexesTag removeObject:[idtag objectAtIndex:indexPath.row]];
        i--;
    }
4

1 回答 1

1

试试这个代码:

UITableViewCell *thisCell = [tablename cellForRowAtIndexPath:indexPath];
    NSMutableArray *selectedIndexes=nil;
    if ([thisCell accessoryType] == UITableViewCellAccessoryNone)
    {
        if([selectedIndexes count]>2)
        {
               //alert view
        }
        else
        {
            [thisCell setAccessoryType:UITableViewCellAccessoryCheckmark];
             [selectedIndexes addObject:[catarray objectAtIndex:indexPath.row]];
            [selectedIndexesTag addObject:[idtag objectAtIndex:indexPath.row]];

        }
                }
    else
    {
         [thisCell setAccessoryType:UITableViewCellAccessoryNone];
        [selectedIndexes removeObject:[catarray objectAtIndex:indexPath.row]];
        [selectedIndexesTag removeObject:[idtag objectAtIndex:indexPath.row]];


    }
     [btncat setTitle:[selectedIndexes componentsJoinedByString:@","] forState:UIControlStateNormal]
于 2013-04-29T14:04:17.410 回答