当按下button时,需要访问UITableViewCell的detailTextLabel属性

我已经以编程方式创build了UITableView,并且还以编程方式创build了包含在每个单元格/行中的UIButton。 我已经设置了这些button,如果他们被轻敲,他们的button图像改变。 所以当用户第一次看到表格视图时,这些button是灰色的,但是如果用户点击其中一个button,那么这个button就会变成红色。 如果他们再次点击它,它会变回灰色。

我需要这样做,如果一个button已被按下,并且正在使用红色图像,那么它会将其单元格的detailTextLabel属性值传递给一个NSMutableArray对象。

这是我的代码,控制了大部分的UITableView。 这是单元格的textLabels和detalTextLabels设置的位置:

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]; 

上述代码中最重要的部分是这些语句:

 [addUserButton setImage:addUserButtonImage forState:UIControlStateNormal]; [addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateHighlighted]; [addUserButton setImage:addUserButtonImageHighlighted forState:UIControlStateSelected]; 

以上控制button不同状态的设置,这有助于使button在按下时变成灰色图像或红色图像。

下面的语句是一个处理“触摸”事件的方法调用,当按下button时,button从灰色图像变为红色图像:

 [addUserButton addTarget:self action:@selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 

以下是我的View Controller中的上述方法的实现:

 - (void)handleTouchUpInside:(UIButton *)sender { sender.selected = !sender.selected; NSLog(@"Has sender state changed?: %u", sender.state); } 

你会注意到我正在使用NSLog来打印button的状态。 每次按下某个button时,“状态”属性都会改变它的值。 现在,我需要找出一种方法,以便如果特定button的状态发生变化,我们将抓住它的单元格的“detailTextLabel”属性,并将其放置在NSMutableArray中。

您可以使用addUserButton.tag属性来保持tableview索引行(1),因此在handleTouchUpInside(2)里面可以findbutton行:

1-在cellForRowAtIndexPath函数中设置indexPath行

 //Keep indexPath.row on Button.tag addUserButton.tag = indexPath.row 

2-在TableView行上查找button

 - (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"; } 

我希望它的作品。

这里是你如何子类UITableViewCell实现这个(精简一点,以节省空间…)的本质。首先.h

 #import <UIKit/UIKit.h> @interface SampleCell : UITableViewCell @property (nonatomic, assign) id delegate; @end @protocol FancyProtocolNameGoesHere - (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell; @end 

还有他们

 #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 

注册这个子类与TableView(记得要声明符合协议),并将委托自-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath

最后的魔法来实现- (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell; 因为你可以在传入的单元格参数上调用[self indexPathForCell:cell]来获得你可能需要的索引path,所以你知道在哪里插入文本到你的NSMutableArray数据对象中。

具有委托协议模式的子类UITableViewCell不是唯一的方法来做到这一点,但我认为这对你所描述的内容是有意义的。

这个解决scheme对表格部分是安全的。

 - (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 */ }