Je veux détecter le bouton appuyez sur un UITableViewCell où le UITableView parent se compose de plusieurs sections.
J'ai été capable de le faire dans le cas d'une section, mais j'ai eu du mal à déterminer la section correcte où il y en avait plus d'une.
J'essaie de déterminer la section correcte correspondant à UITableViewCell où l'utilisateur a tapé sur le bouton.
Voici le code mis à jour une fois le problème résolu:
@property (strong, nonatomic) NSMutableArray* quantityArray;
@property (strong, nonatomic) NSMutableArray* rows;
@synthesize quantityArray,rows;
- (void)viewDidLoad {
[super viewDidLoad];
quantityArray = [[NSMutableArray alloc] init];
[self PluMinusFunction];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
// Some code here.
//
if (addBtnClicked || minusBtnClicked) {
cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
addBtnClicked = NO;
minusBtnClicked = NO;
} else {
cell.lblCount.text = [[quantityArray objectAtIndex:indexPath.section-1]objectAtIndex:indexPath.row];
}
cell.btnMinus.tag = indexPath.row;
cell.btnPlus.tag = indexPath.row;
cell.lblCount.tag = indexPath.row;
[cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
if ([ cell.lblCount.text integerValue]!=0) {
[cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
}
#pragma mark - Plus Minus Button
- (void)PluMinusFunction {
for (int j=0; j <[itemArr count]; j++) {
rows = [[NSMutableArray alloc] init];
for (int i=0; i <[ [[itemArr objectAtIndex:j] objectForKey:@"itemList"]count]; i++) {
[rows insertObject:@"0" atIndex:i];
}
[quantityArray insertObject:rows atIndex:j];
}
[self sum];
}
#pragma mark - UIButton selector
- (void)addItem:(UIButton*)button {
CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
NSInteger row = clickedButtonIndexPath.row;
NSInteger section = clickedButtonIndexPath.section;
NSInteger LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] + 1;
NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
[subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];
[quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
addBtnClicked = YES;
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
- (void)deleteItem:(UIButton*)button {
CGPoint touchPoint = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
NSInteger row = clickedButtonIndexPath.row;
NSInteger section = clickedButtonIndexPath.section;
NSInteger LabelValue =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue];
if (LabelValue >= 1) {
NSInteger LabelText =[[[quantityArray objectAtIndex:section-1]objectAtIndex:row]integerValue] - 1;
NSMutableArray *subArray = [quantityArray objectAtIndex:section-1];
[subArray replaceObjectAtIndex:row withObject: [NSString stringWithFormat:@"%ld",(long)LabelText]];
[quantityArray replaceObjectAtIndex:section-1 withObject: subArray];
addBtnClicked = YES;
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:row inSection:section];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
}
Je veux réaliser la mise en page comme indiqué ci-dessous:
Ce lien a également apporté la solution à mon problème:
Objectif c
-(void)addItem:(UIButton*) sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:mainTable]; // maintable --> replace your tableview name
NSIndexPath *clickedButtonIndexPath = [mainTable indexPathForRowAtPoint:touchPoint];
NSLog(@"index path.section ==%ld",(long)clickedButtonIndexPath.section);
NSLog(@"index path.row ==%ld",(long)clickedButtonIndexPath.row);
}
Swift3
func addItem(sender: UIButton)
{
var touchPoint = sender.convert(CGPoint.zero, to: self.maintable)
// maintable --> replace your tableview name
var clickedButtonIndexPath = maintable.indexPathForRow(at: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
}
Swift2 et au-dessus
func addItem(sender: UIButton)
{
var touchPoint: CGPoint = sender.convertPoint(CGPointZero, toView: mainTable)
// maintable --> replace your tableview name
var clickedButtonIndexPath: NSIndexPath = mainTable(forRowAtPoint: touchPoint)
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath.row))
}
Swift 3
let clickedButtonIndexPath = self.tableView.indexPathForRow(at: touchPoint)
if(clickedButtonIndexPath != nil)
{
NSLog("index path.section ==%ld", Int(clickedButtonIndexPath!.section))
NSLog("index path.row ==%ld", Int(clickedButtonIndexPath!.row))
}
Un moyen plus direct d'obtenir le chemin de l'index sans utiliser CGPoint
et émettre des hypothèses sur l'interface utilisateur consiste à examiner le .superview
. J'imagine que c'est plus fiable en pratique.
- (myTableViewCell *)cellContainingView:(UIView *)view
{
do {
view = view.superview;
} while (view != nil && ![view isKindOfClass:[myTableViewCell class]]);
return (myTableViewCell *)view;
}
Ensuite, vous pouvez simplement faire ceci dans l'action du bouton:
- (IBAction)myButtonPressed:(id)sender
{
myTableViewCell *cell = [self cellContainingView:(UIView *)sender];
NSIndexPath *path = [self.tableView indexPathForCell:cell];
}