iPhone SDK: Opening a cell with a dedicated button, not by cell tapping

旧城冷巷雨未停 提交于 2019-11-29 12:49:41

I do something similar in an app I am working on right now. I have a cell that has a button on it, and I need to know which button was pushed in which cell. I do that like this..

I add my button to each cell..

// add buy button to each cell
UIImage *image;
buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
image = [UIImage imageNamed:@"buy.png"];
[buyButton setBackgroundImage:image forState:UIControlStateNormal];
buyButton.frame = CGRectMake(220, 35, 96, 34);
[buyButton setTag:cellIndex];
[buyButton addTarget:self action:@selector(buyTickets:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:buyButton];

The method used to determine which "button" in which cell was selected, I then push another view controller with the information of the selected button...

// buy tickets button pressed from main table view 
- (void) buyTickets:(id)sender{


    ResultViewController *vc = [[ResultViewController alloc] init];
    vc.buyMovieID = [sender tag]; // "sender tag" is the cell id the button is located in
    [[super navigationController] pushViewController:vc animated:YES];
    [vc release];
}

This is what the button looks like on each cell. Hope this helps!

P.S. Tapping on the CELL, would push another view controller, but tapping on "Buy Tickets" button pushes a different one.

alt text http://luistovar.com/ultratableview.jpg

I think the detail disclosure accessory type is what you need. The doc can be found on UITableViewCell class reference.

It says :

The accessory view appears in the the right side of the cell in the table view’s normal (default) state. The standard accessory views include the disclosure chevron; for a description of valid accessoryType constants, see “Cell Accessory Type.” The default is UITableViewCellAccessoryNone. If a custom accessory view is set through the accessoryView property, the value of this property is ignored. If the cell is enabled and the accessory type is UITableViewCellAccessoryDetailDisclosureButton, the accessory view tracks touches and, when tapped, sends the data-source object a tableView:accessoryButtonTappedForRowWithIndexPath: message.

Setting your cell's accessoryType property to UITableViewCellAccessoryDetailDisclosureButton, you can easily do whatever you wan when the detail disclosure button is pressed. What's great about using this accessory type is that it's a standard button, so it is user-friendly and it does all the job of tracking which cell has been touched for you.

If you want to use a custom button, you should set the accessoryView property to that custom button and listen to events on it.

The way i added a button to a tableview cell was by subclassing UITableViewCell and then building its view in interface builder.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!