我已经以编程方式创建了一个UITableView,并且我还以编程方式创建了UIButtons以包含在每个单元格/行中。我已经设置了这些按钮,如果它们被点击,它们的按钮图像就会改变。因此,当用户第一次看到表格视图时,按钮颜色为灰色,但如果用户点击其中一个按钮,则该按钮将变为红色。如果他们再次点击它,它将变回灰色。
我需要这样做,以便如果按下按钮,并且当前正在使用红色图像,那么它会将它的单元格的detailTextLabel属性值传递给NSMutableArray对象。
这是我的代码,它控制着大多数UITableView。这是设置单元格的textLabels和detalTextLabel的地方:
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];
以上控制按钮不同状态的设置,这有助于按钮在按下时变为灰色图像或红色图像。
以下语句是一个方法调用,用于处理“触摸”事件,并在按下按钮时将按钮从灰色图像更改为红色图像:
[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打印出按钮的状态。每当按下一个按钮时,“state”属性就会改变它的值。现在我需要弄清楚如何改变特定按钮的状态,我们将获取它的单元格的“detailTextLabel”属性并将其放在NSMutableArray中。
答案 0 :(得分:1)
您可以使用addUserButton.tag属性来保持tableview索引行(1),因此在handleTouchUpInside(2)中您可以找到按钮行:
1-在cellForRowAtIndexPath函数中设置indexPath行
//Keep indexPath.row on Button.tag
addUserButton.tag = indexPath.row
TableView Rows上的2-查找按钮
- (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";
}
我希望它有效。
答案 1 :(得分:0)
这是你如何继承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
而.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
使用TableView注册此子类(记得声明符合协议),并在-(UITableViewCell*)cellForRowAtIndexPath:(NSIndexPath*)indexPath
实现- (void)doSomethingWithThisText:(NSString*)text fromThisCell:(UITableViewCell*)cell;
时最后的魔法来了,因为您可以在传入的单元格参数上调用[self indexPathForCell:cell]
来获取您可能需要的索引路径,这样您就知道在NSMutableArray中插入文本的位置数据对象。
带有委托协议模式的子类UITableViewCell并不是唯一的方法,但我认为它对你所描述的内容有所帮助。
答案 2 :(得分:0)
此解决方案对于表格部分是安全的。
- (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 */
}