SORU
26 Kasım 2009, PERŞEMBE


Bir UİTableView içinde hangi tuşa basıldığını tespit

5 UITableViewCells UITableView var. Her hücre aşağıdaki gibi kurulur UIButton içerir

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
         cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
         [cell autorelelase];

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
         [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
         [button setTag:1];
         [cell.contentView addSubview:button];

         [button release];
     }

     UIButton *button = (UIButton *)[cell viewWithTag:1];
     [button setTitle:@"Edit" forState:UIControlStateNormal];

     return cell;
}

Benim sorum şu: buttonPressedAction: yöntem, nasıl bir baskı olmuştur. Etiketleri kullanarak düşündüm ama bu en iyi yol olduğundan emin değilim. Bir şekilde kontrol üzerine indexPath etiketi için mümkün olmak istiyorum.

- (void)buttonPressedAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    // how do I know which button sent this message?
    // processing button press for this row requires an indexPath. 
}

Bunu yapmanın standart bir yolu nedir?

Düzenleme:

Biraz aşağıdaki şekilde çözdüm. Hala bu işin standart bir yolu olup olmadığını bir fikre sahip olmak istiyorum yoksa daha iyi bir yolu var mı?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
         cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
         [cell autorelelase];

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
         [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
         [cell.contentView addSubview:button];

         [button release];
     }

     UIButton *button = (UIButton *)[cell.contentView.subviews objectAtIndex:0];
     [button setTag:indexPath.row];
     [button setTitle:@"Edit" forState:UIControlStateNormal];

     return cell;
}

- (void)buttonPressedAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    int row = button.tag;
}

ÖNEMLİ NOT için ne hücre yerine dequeued, çünkü hücre oluşturulmasında etiketi uyduramam. Çok adice geliyor. Daha iyi bir yolu olmalı.

CEVAP
26 Kasım 2009, PERŞEMBE


Elma Accessory örnek aşağıdaki yöntem kullanılır:

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

Temas işleyici dokunmatik alınan koordinat ve dizin yolunu koordine eden hesaplanır:

- (void)checkButtonTapped:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ...
    }
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ★TheCrono Official Channel★

    ★TheCrono

    3 Mayıs 2014
  • bashirsultani

    bashirsultan

    22 Mart 2010
  • SuperPrincessjo

    SuperPrinces

    1 EKİM 2010