Я пытаюсь понять это в течение некоторого времени. Я программно создаю UIButton в ячейке таблицы. После нажатия кнопки я хочу перейти или нажать на другой контроллер представления. Это проще простого, если я использую StoryBoard, создаю там кнопку, а затем, удерживая нажатой клавишу Control, перетаскиваю переход к целевому VC. Однако я не хочу делать это таким образом по разным причинам. Ниже мой код до сих пор.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// <<<< bunch of code snipped out here to keep this example short >>>
for(UIButton *button in cell.subviews)
{
[button removeFromSuperview];
}
UIImage *image = [UIImage imageNamed:[contentArray_ objectAtIndex:indexPath.row]];
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 240, 240)];
[imageButton setImage:image forState:UIControlStateNormal];
imageButton.layer.masksToBounds = YES;
imageButton.layer.cornerRadius = 5.0;
imageButton.layer.borderWidth = 1.0;
imageButton.layer.borderColor = [[UIColor grayColor] CGColor];
CGAffineTransform rotateButton = CGAffineTransformMakeRotation(M_PI_2);
imageButton.transform = rotateButton;
cell.backgroundColor = [UIColor clearColor];
[cell addSubview:imageButton];
return cell;
Теперь для каждой строки в этой таблице будет программно создана кнопка с изображением. Я также вращаю кнопку - да, это горизонтальный вид таблицы.
Итак, как я могу реагировать на нажатия кнопок? Все кнопки будут идти к одному и тому же контроллеру представления назначения...
PS - я использую Xcode 4.2 с ARC (не уверен, что это действительно имеет значение).
----- Обновление вопроса
Я реализовал предложение кода (спасибо jrturton), однако я считаю, что, поскольку я нахожусь в ячейке UITableView, я не могу представить контроллер модального представления. Теперь у меня есть следующий код...
.h файл:
@interface DetailsHorizontalPhotoCell : UITableViewCell <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) IBOutlet UITableView *horizontalTableView;
@property (nonatomic, strong) NSArray *contentArray;
- (IBAction)photoButton:(id)sender;
@end
.м файл:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/// ----- code here to setup the cell, button, image, etc...
// do something when the buttong is pressed
[imageButton addTarget:self action:@selector(photoButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)photoButton:(id)sender
{
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.horizontalTableView];
NSIndexPath *hitIndex = [self.horizontalTableView indexPathForRowAtPoint:hitPoint];
// do some stuff here to pull object data out of the contentArray
NSLog(@"Image clicked, index: %d", hitIndex.row);
// other non-relevant code snipped to keep this example short
// next I want to present a modal view controller (lets call it PhotoAlbum) which is a TableViewController
PhotoAlbum *photoAlbum = [[PhotoAlbum alloc] init];
// below does not work, does not even autocomplete in Xcode. I believe its because I'm in a UITableViewCell and not a VC... Any suggestions here?
[photoAlbum setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:photoAlbum animated:YES];
}