当用户从表格视图中取消选择该值并从标题名称中删除时,我想在用户从表格视图中选择行时设置按钮标题。
这是我的代码。 我想只选择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--;
}
答案 0 :(得分: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]