how to get perform action in table cell button
In my application i have created a UIViewController page in that i have taken a table. by customized that table i have created a add button in each cell of that table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"CustomBuddy" owner:self options:nil];
cell = self.customCell;
self.customCell = nil;
}
cell.text = (NSString *)[names objectAtIndex:indexPath.row];
return cell;
}
and add action to the add button
-(IBAction)addButtonClicked
{
NSLog(@"Add button clicked");
}
now in this add button i want to get the name exist in the开发者_JS百科 cell. how can i do this please help me out....
You could assign the tag for your Add button during your tableView:cellForRowAtIndexPath
creation. Then you can just readout the Add button tag when it's triggered. By the way, it should be -(IBAction) addButtonClicked:(id) sender
check iPhone dynamic UIButton in UITableView
there you will see how to add and similarly assigning tag
You can subclass UITableViewCell and add a UIButton property to it, then also add your addButton handler code to the tableview cell as well and link the button action to the button in your nib. If you need a handle to the UITableViewController keep an outlet to that in your custom table view cell as well. Just be aware that your cell might get cleaned up or reused if the user scrolls while your action handling code is running so be careful about referencing back to the table cell from within your action handling code (e.g. if the action handling code loses control at any point).
Alternately you can put the action handler in your view controller as you have done above, then get the visible cells in the action handler and loop through them finding out which one matches the superview for your button view.
Or alternately the approach of using the button tag as a way of passing an object index for use by your action handler as suggested in the other posts seems sensible as well if you only need an index to look up the underlying object you want to manipulate in the action handler.
A simple solution would be changing the addButtonClicked
method to
-(IBAction)addButtonClicked:(NSInteger)row{
NSLog(@"Add button clicked");
}
and when calling the selector yu can the pass indexPath.row
value.
Thereby you can fetch it back from the array.
Hope this helps
精彩评论