按下按钮时需要访问 UITableViewCell 的 detailTextLabel 属性 [英] Need to access detailTextLabel property of UITableViewCell when button is pressed

查看:67
本文介绍了按下按钮时需要访问 UITableViewCell 的 detailTextLabel 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式创建了一个 UITableView 并且我还以编程方式创建了 UIButtons 以包含在每个单元格/行中.我已经设置了这些按钮,以便在点击它们时,它们的按钮图像会发生变化.所以当用户第一次看到 table view 时,按钮是灰色的,但是如果用户点击其中一个按钮,那么那个按钮就会变成红色.如果他们再次点击它,它会变回灰色.

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.

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

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.

这是我控制大部分 UITableView 的代码.这是设置单元格的 textLabels 和 detalTextLabels 的地方:

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);   
}

您会注意到我使用 NSLog 来打印按钮的状态.每次按下按钮之一时,状态"属性都会更改其值.现在我需要找到一种方法来实现它,以便如果特定按钮的状态发生变化,我们将获取它的单元格的detailTextLabel"属性并将其放置在 NSMutableArray 中.

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.

推荐答案

你可以使用 addUserButton.tag 属性来保持 tableview 索引行(1),所以在 handleTouchUpInside(2)里面你可以找到按钮行:

You can use the addUserButton.tag property to keep tableview index row (1), so inside the handleTouchUpInside (2) you can find button row:

1- 在 cellForRowAtIndexPath 函数内设置 indexPath 行

1- Set indexPath row inside the cellForRowAtIndexPath function

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

2- 在 TableView Rows 上查找按钮

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

}

我希望它有效.

这篇关于按下按钮时需要访问 UITableViewCell 的 detailTextLabel 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆