iPhone SDK: пользовательские кнопки в TableView - PullRequest
2 голосов
/ 15 августа 2010

Я использую пользовательские кнопки в табличном представлении, и это хорошо работает для меня:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UIImage *detailsButtonImage;
     UIButton *detailsButton;

     NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
     }

     //populate cells with array elements
     cell.textLabel.text = [itemsArray objectAtIndex:indexPath.row];

     //a custom button
     detailsButton = [UIButton buttonWithType:UIButtonTypeCustom];
     detailsButtonImage = [UIImage imageNamed:@"details.png"];
     [detailsButton setBackgroundImage:detailsButtonImage forState:UIControlStateNormal];
     detailsButton.frame = CGRectMake(275, 10, 20, 22);

      //which cell was tapped?
     [detailsButton setTag:[itemsArray objectAtIndex:indexPath.row]];
     [detailsButton addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
     [cell.contentView addSubview:detailsButton];
      return cell;
 }

//showing details of selected item
- (void) showDetails:(id)sender
{  
    AnotherViewController *anotherViewController = [[AnotherViewController alloc] init];
    anotherViewController.title = [sender tag];
    anotherViewController.itemDescription = [itemsDescriptions objectAtIndex:[itemsArray indexOfObjectIdenticalTo:[sender tag]]]; 

    [self.navigationController pushViewController:anotherViewController animated:YES];
    [anotherViewController release];
}

Мне интересно, есть ли другой способ отправки идентификатора ячейки в AnotherViewController, кроме установки тега.

1 Ответ

1 голос
/ 16 августа 2010

Похоже, вы хотите, чтобы showDetails знал, какая кнопка его нажала. Я думаю, что вы делаете это хорошо, но если бы вы хотели, вы могли бы иметь функцию

  • (void) showDetails: (id) отправитель withObject: (id) объект;

и вызовите это с помощью кнопки, а не просто showDetails: (id) sender; Я предполагаю, что ваши объекты являются строками того, что вы делаете, так что это может быть

  • (void) showDetails: (id) отправитель withString: (NSString *) строка;

Но, если честно, то, как вы это делаете сейчас, прекрасно, зачем менять?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...