问题
I have programmatically created a UITableView and I have also programmatically created UIButtons to be included with every cell / row. I have set these buttons so that if they are tapped, their button image changes. So when the user first sees the table view, the buttons are the color grey, but if the user taps one of the buttons, then that button will turn red. If they tap it again, it will turn back to grey.
I need to make it so that if a button has been pressed, and is currently using the red image, then it will pass it's cell's detailTextLabel property value into an NSMutableArray object.
Here is my code that controls the majority of the UITableView. This is where the cell's textLabels and detalTextLabels are set:
userName = [self.potentiaFriendsInParseFirstNamesArray objectAtIndex:indexPath.row];
NSString *firstNameForTableView = [self.potentiaFriendsInParseFirstNamesArray objectAtIndex:indexPath.row];
NSString *userNameForTableView = [self.potentiaFriendsUsernameArray objectAtIndex:indexPath.row];
UIImage *addUserButtonImage = [UIImage imageNamed:@"SliderThumb-Normal-G"];
UIImage *addUserButtonImageHighlighted = [UIImage imageNamed:@"SliderThumb-Normal"];
UIButton *addUserButton = [[UIButton alloc]init];
addUserButton.frame = CGRectMake(237, -10, 64, 64);
[addUserButton setImage:addUserButtonImage forState:UIControlStateNormal];
[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateHighlighted];
[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateSelected];
[addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[cell.textLabel setText:firstNameForTableView];
[cell.detailTextLabel setText:userNameForTableView];
[cell.contentView addSubview:addUserButton];
The most important parts of the above code are these statements:
[addUserButton setImage:addUserButtonImage forState:UIControlStateNormal];
[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateHighlighted];
[addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateSelected];
The above control the settings for the different states of the button, and this helps make the button turn to the grey image or the red image when it is pressed.
The below statement is a method call that handles the "touch" events and makes the button change from the grey image to the red image when it is pressed:
[addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
And here is the above method's method implementation in my View Controller:
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;
NSLog(@"Has sender state changed?: %u", sender.state);
}
You will notice that I am using NSLog to print out the button's state. The "state" property changes it's value every time one of the button's is pressed. Now I need to figure out a way to make it so that if a specific button's state is changed, we will grab it's cell's "detailTextLabel" property and place it inside an NSMutableArray.
回答1:
You can use the addUserButton.tag property to keep tableview index row (1), so inside the handleTouchUpInside (2) you can find button row:
1- Set indexPath row inside the cellForRowAtIndexPath function
//Keep indexPath.row on Button.tag
addUserButton.tag = indexPath.row
2- Find button on TableView Rows
- (void)handleTouchUpInside:(UIButton *)sender {
UIButton *cellButton = (UIButton *)sender;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:cellButton.tag inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
cell.detailTextLabel.text = @"teste";
}
I hope it works.
回答2:
Here's the essence of how you'd subclass UITableViewCell to accomplish this (condensed a bit to save space...) First the .h
#import <UIKit/UIKit.h>
@interface SampleCell : UITableViewCell
@property (nonatomic, assign) id delegate;
@end
@protocol FancyProtocolNameGoesHere
- (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell;
@end
And the .m
#import "SampleCell.h"
@interface SampleCell ()
@property (strong, nonatomic) UIButton *button;
- (void)handleTouchUpInside;
@end
@implementation SampleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self configureCell];
}
return self;
}
- (void)configureCell
{
_button = [[UIButton alloc] init];
//Configure all the images etc, for the button
[_button addTarget:self action:@selector(handleTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button]; //you'd probably want to set the frame first...
}
- (void)handleTouchUpInside
{
if (_button.selected) {
[self.delegate doSomethingWithThisText:self.textLabel.text fromThisCell:self];
}
_button.selected = !_button.selected;
}
@end
Register this subclass with the TableView (remember to declare conformity to the protocol), and assign the delegate to self within -(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath
The final magic comes as you implement - (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell;
because you can call [self indexPathForCell:cell]
on the cell parameter you passed in to get the index path you probably need so you know where to insert the text in your NSMutableArray data object.
A subclassed UITableViewCell with a delegate protocol pattern is not the only way to do this, but I think it makes some sense for what you described.
回答3:
This solutions is safe to table sections.
- (void)onButtonPressed:(UIButton *)sender event:(id)event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForItemAtPoint:point];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
/* use cell here */
}
来源:https://stackoverflow.com/questions/22029931/need-to-access-detailtextlabel-property-of-uitableviewcell-when-button-is-presse