UIButton 条件适用于单个索引路径行
UIButton condition works in single index path row
我正在创建一个足球比赛应用程序,我在 UITableviewcell 中有两个按钮,我正在检查其条件,我的条件适用于整个 table 视图。我的问题是:我的条件应该适用于 table.see 我在那个 L 按钮 select 中的图像的每一行意味着 V 按钮不应该改变它在第 0 个索引路径中的工作,但是当我 select V 按钮没有改变,我的条件应该适用于每个索引路径行。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
button1.tag = 56;
[cell.contentView addSubview:button1];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
button3.tag = 57;
[button3 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
}
- (void)radiobtn2:(UIButton *)button
{
if (button.tag == 56)
{
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:@"l.png"]
forState:UIControlStateNormal];
NSLog(@" select");
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
NSLog(@"not select");
[button setSelected:YES];
}
}else
{
}
if (button.tag == 57)
{
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:@"v.png"]
forState:UIControlStateNormal];
NSLog(@"select");
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
NSLog(@"not select");
[button setSelected:YES];
}
} else
{
NSLog(@"button3 == 1");
}
。请帮助我,我应该听到什么,我很挣扎,请任何人帮助我编码。 我需要这样的条件
您不应为所有行设置相同的按钮标签。为单个按钮设置 button.tag = 1000+indexPath.row
,为 V 按钮设置 2000+indexPath.row
(以便将它们分开)。因此,当操作被触发时,您知道它属于哪一行,并且只有那一行才会被设置为选中。
添加了一些更改,请查看
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
[button1 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[cell.contentView addSubview:button1];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
[button3 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button3.tag=2;
}
- (void)radiobtn2:(UIButton *)sender
{
UITableViewCell *cell=(UITableViewCell *)sender.superview;
sender.selected=YES;
if(sender.tag==1){
UIButton *otherButton=(UIButton *)[cell viewWithTag:2];
otherButton.selected=NO;
}else if(sender.tag==2){
UIButton *otherButton=(UIButton *)[cell viewWithTag:1];
otherButton.selected=NO;
}
}
这应该适合您。但是你应该为这样的设计使用自定义单元格,并使用数据来切换状态..
干杯。
I think your problem is with button tag. from your current code every
buttons will get same tag..
do this
declare a variable glabally, NSIndexPath *butIndexPath;
in cellForRow method ,after cell==nil method
put this code
butIndexPath.row=2*indexpath.row;
then use
button1.tag=butIndexPath.row;
button3.tag=butIndexPath.row.+1;
update button action method with below code
change button.tag == 56 into button.tag == butIndexPath.row
and button.tag == 57 into button.tag == butIndexPath.row+1
您想要在 table 中逐个选择按钮图像(第一行 "L" 看起来已选中,第二行 "V" 看起来已选中,第三行 "L",第四行"V", 等..)
然后您想获取特定行的特定按钮的状态。我猜对了吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
[button1 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[cell.contentView addSubview:button1];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
[button3 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button3.tag=2;
}
if ((indexPath.row % 2) == 0) {
// Make left button selected by default by using setSelected property
}
else
{
// Make right button selected by default by using setSelected property
}
}
So now when ever you do any action you will get appropriated state
我正在创建一个足球比赛应用程序,我在 UITableviewcell 中有两个按钮,我正在检查其条件,我的条件适用于整个 table 视图。我的问题是:我的条件应该适用于 table.see 我在那个 L 按钮 select 中的图像的每一行意味着 V 按钮不应该改变它在第 0 个索引路径中的工作,但是当我 select V 按钮没有改变,我的条件应该适用于每个索引路径行。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
button1.tag = 56;
[cell.contentView addSubview:button1];
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
button3.tag = 57;
[button3 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
}
- (void)radiobtn2:(UIButton *)button
{
if (button.tag == 56)
{
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:@"l.png"]
forState:UIControlStateNormal];
NSLog(@" select");
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
NSLog(@"not select");
[button setSelected:YES];
}
}else
{
}
if (button.tag == 57)
{
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:@"v.png"]
forState:UIControlStateNormal];
NSLog(@"select");
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
NSLog(@"not select");
[button setSelected:YES];
}
} else
{
NSLog(@"button3 == 1");
}
您不应为所有行设置相同的按钮标签。为单个按钮设置 button.tag = 1000+indexPath.row
,为 V 按钮设置 2000+indexPath.row
(以便将它们分开)。因此,当操作被触发时,您知道它属于哪一行,并且只有那一行才会被设置为选中。
添加了一些更改,请查看
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
[button1 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[cell.contentView addSubview:button1];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
[button3 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button3.tag=2;
}
- (void)radiobtn2:(UIButton *)sender
{
UITableViewCell *cell=(UITableViewCell *)sender.superview;
sender.selected=YES;
if(sender.tag==1){
UIButton *otherButton=(UIButton *)[cell viewWithTag:2];
otherButton.selected=NO;
}else if(sender.tag==2){
UIButton *otherButton=(UIButton *)[cell viewWithTag:1];
otherButton.selected=NO;
}
}
这应该适合您。但是你应该为这样的设计使用自定义单元格,并使用数据来切换状态..
干杯。
I think your problem is with button tag. from your current code every
buttons will get same tag..
do this
declare a variable glabally, NSIndexPath *butIndexPath;
in cellForRow method ,after cell==nil method
put this code
butIndexPath.row=2*indexpath.row;
then use
button1.tag=butIndexPath.row;
button3.tag=butIndexPath.row.+1;
update button action method with below code
change button.tag == 56 into button.tag == butIndexPath.row
and button.tag == 57 into button.tag == butIndexPath.row+1
您想要在 table 中逐个选择按钮图像(第一行 "L" 看起来已选中,第二行 "V" 看起来已选中,第三行 "L",第四行"V", 等..)
然后您想获取特定行的特定按钮的状态。我猜对了吗?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"HistoryCell";
CustomizedCellView *cell = (CustomizedCellView *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomizedCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"l"ofType:@"png"]] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"lblue.png"]
forState:UIControlStateSelected];
[button1 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[cell.contentView addSubview:button1];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(240, 27, 36, 36);
[button3 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"v"ofType:@"png"]] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"vblue.png"]
forState:UIControlStateSelected];
[button3 addTarget:self action:@selector(radiobtn2:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button3];
button3.tag=2;
}
if ((indexPath.row % 2) == 0) {
// Make left button selected by default by using setSelected property
}
else
{
// Make right button selected by default by using setSelected property
}
}
So now when ever you do any action you will get appropriated state