UIButton 条件适用于单个索引路径行
Posted
技术标签:
【中文标题】UIButton 条件适用于单个索引路径行【英文标题】:UIButton condition works in single index path row 【发布时间】:2015-03-11 11:24:21 【问题描述】:我正在创建一个足球比赛应用程序,我在 UITableviewcell 中有两个按钮,我正在检查条件,我的条件适用于整个表格视图。我的问题是:我的条件应该适用于表格的每一行。看到我的图像,L 按钮选择意味着 V 按钮不应该改变它在第 0 个索引路径中的工作,但是当我选择 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");
。请帮助我,我应该听到什么,我很挣扎请任何人帮助我编码。 我需要这样的条件
【问题讨论】:
请说明您的要求 这不是为 UIButton 添加选择器的准确方法。如果您可以在 CustomizedCellView Cell 中编写 radiobtn2 方法登录,那么您的工作就可以完成。 我的要求就像第二张图片。我想检查每一行的条件。当我选择 L 按钮时,v 按钮不应该改变,当我选择 V 按钮时,L 按钮不应该改变。 【参考方案1】:添加了一些更改,请检查
- (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;
这应该适合你。但是,您应该对这样的设计使用自定义单元格,并使用数据来切换状态..
干杯。
【讨论】:
兄弟我想检查两个按钮之间的条件。我的要求就像第二张图片。我想检查每一行的条件。当我选择 L 按钮时,v 按钮不应该改变,当我选择 V 按钮时,L 按钮不应该改变。 @Raj 你的意思是,如果一个人不应该选择另一个人,对吧? 我的头像没变啊兄弟 您应用了我的更改吗?它应该可以工作,这是正确的实现。我已经测试过了。 您需要在创建按钮时为选定状态设置图像,然后只有它才能工作.. 看到这个 [button3 setImage:[UIImage imageNamed:@"vblue.png"] forState:UIControlStateSelected ];请正确查看我的答案。您还需要在 if(cell==nil) 块内创建按钮。【参考方案2】:您不应为所有行设置相同的按钮标签。为一键设置button.tag = 1000+indexPath.row
,为V键设置2000+indexPath.row
(以便将它们分开)。所以当动作被触发时,你就知道它属于哪一行,只有那一行才会被设置为选中。
【讨论】:
相同的答案 :-) 为每一行定义不同的按钮标签。【参考方案3】: 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
【讨论】:
【参考方案4】:您希望在表格中逐个选择按钮图像(第一行“L”看起来已选择,第二行“V”看起来已选择,第三行“L”,第四行“V”等)
然后你想获得特定行的特定按钮的状态。我说对了吗?
【讨论】:
检查我上面的答案。我在此之后发布了它仍然出现在此评论上方。无论如何只是寻找那个。希望它会有所帮助【参考方案5】:- (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
【讨论】:
以上是关于UIButton 条件适用于单个索引路径行的主要内容,如果未能解决你的问题,请参考以下文章
如何在运行时为 UIBarButtonItem 设置目标和操作