How to implement custom cell selection behavior with button for UITableViewController?
I'm trying to replace standard table cell selection behavior with a button-press behavior. The result should be same - to push detailViewController
on top and to present some detail info. The cell view is loaded from a separate xib file. So, I need to put a button in that cell xib and use that button for opening detail VC in table VC. I have placed the button view in cell xib, and disabled cell selection in table VC viewDidLoad, but now, I'm not sure how to handle that button press, where to place the button press code or what delegate method to use because didSelectRowAtIndexPath
is not respoding (cos I disabled it). Any suggestions?
UPDATE: I need to get some info from cell (section, index) on button press in order to initialize the data for detail VC. Also, should note, that there will be several buttons in each cell.
UPDATE2: I'm using rows with sections, so on button press I need to pass both section and row numbers that corresponds to cell where the button was pressed.
UPDATE3: I have tried to use unique tags for identifying cells but it looks like it's not the solution because I'm getting problems with cell reusing. The first 9 rows are created and relevant tag number are set to them. But then every row from 10 is reused from the queue, so I get the same 9 view with their tags. For example, 10th row pops me the first row button from the queue with the tag 1, 11th - second row button with tag 2 and so on. I mean, the whole dequeue cells means that it doesn't matter how much rows do you have in table, there will be reused 9 cell views for all of the table rows. Th开发者_如何转开发at means I cannot assign unique tags for the cells because the cell views are not unique for each row
If i got u r question right then this will be solution
[cell.yourButton addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
//you can use tag property of button
NSUInteger tag=indexPath.row+1;
cell.yourButton.tag=tag;
this code should be in
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
& you write action as
-(IBAction)myAction:(id)sender{
UIButton *but=(UIButton *)sender;
NSLog(@"Tag-%d",but.tag);
NSUInteger index=but.tag-1;
//using tag you will come to know which button is pressed ...
}
The suggestions with tags didn't worked out because of a reason described in UPDATE3. However, I did succeed by calling "magic" indexPathForRowAtPoint: method. So the solutions is:
1) attach custom method for button press in cellForRowAtIndexPath:
// I have already created/loaded and got a handler to my button btn
// here comes attaching
[btn addTarget:self action:@selector(openDetailViewController:event:) forControlEvents:UIControlEventTouchUpInside];
2) Define custom method:
-(void)openDetailViewController:(id)sender event:(UIEvent *)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
// do my stuff
}
So, when the button gets pressed my method gets called, passing some additional parameter like event. By calling "allTouches" and "anyObject" I'm getting the touch. Then, I'm retrieving CGPoint from that touch, then passing that point to "indexPathForRowAtPoint" that returns desired indexPath with section and row number. Cool :)
精彩评论