2015-07-27 5 views
13

Chcę wykryć przycisk kliknij na widok tabeli z wieloma wierszami i sekcjami. Udało mi się to osiągnąć w przypadku pojedynczego rzędu, ale mam problem z tym, że nie wykryłem właściwej sekcji. mam kłopoty, aby wykryć, który przycisk sekcja ma użytkownik kliknąłprzycisk wykrywania kliknij w widoku tabeli kod ios dla wielu wierszy i sekcji

poniżej jest zaktualizowany kod po problem jest rozwiązany

@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 { 
    ==================================================== 

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


} 

Chcę acheive jak obrazek poniżej

enter image description here

poniższy link podać rozwiązanie do mojego Problem również

Detecting which UIButton was pressed in a UITableView

Odpowiedz

38

Cel 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 przede

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)) 
} 
+0

Mam problem > w niektórych przypadkach jakieś pomysły ab że bro –

+0

Dzieje się tak w sytuacji losowej, który scenariusz u wychodził –

+0

u można sprawdzić na to pytanie http : //stackoverflow.com/questions/31692692/what-could-we-do-in-case-custom-cell-in-view-headersection-to-reload-part –

1
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)) 
      } 
Powiązane problemy