以下是来自cellForRowAtIndexPath
的代码片段。我在Cell上添加了2个按钮。
当用户点击按钮A
时,按钮A
的图片应为a.png
。按钮B
应更改为default.png
。
当用户点击按钮B
时,按钮B
的图片应为b.png
。按钮A
应更改为default.png
。
我该怎么做?
cellForRowAtIndexPath
方法
[cell.aButton addTarget:self action:@selector(methodA:) forControlEvents:UIControlEventTouchUpInside];
[cell.bButton addTarget:self action:@selector(methodB:) forControlEvents:UIControlEventTouchUpInside];
- (void) methodA:(id) sender {
[((UIButton *)sender) setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateSelected];
// How to change the image of Button B to default.png ??
}
- (void) methodB:(id) sender {
[((UIButton *)sender) setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];
// How to change the image of Button A to default.png ??
}
答案 0 :(得分:0)
如果您为按钮设置了标记,例如aButton
tag
= 1,bButton
tag
= 2
- (void) methodB:(id) sender {
UIButton *bButton =((UIButton *)sender) ;
[bButton setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];
UIButton *aButton = (UIButton *)[bButton.superview viewWithTag:bButton.tag-1];
[aButton setImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateSelected];
}
- (void) methodA:(id) sender {
UIButton *aButton =((UIButton *)sender) ;
[aButton setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateSelected];
UIButton *bButton = (UIButton *)[bButton.superview viewWithTag:aButton.tag+1]; //updated here, instead of bButton, put aButton
[bButton setImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateSelected];
}
享受编码!!
答案 1 :(得分:0)
试试这个......
的cellForRowAtIndexPath
[cell.aButton addTarget:self action:@selector(methodToggle:) forControlEvents:UIControlEventTouchUpInside];
[cell.bButton addTarget:self action:@selector(methodToggle:) forControlEvents:UIControlEventTouchUpInside];
[cell.aButton setImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
[cell.bButton setImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
[cell.aButton setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateSelected];
[cell.bButton setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];
cell.aButton.tag == 111;
cell.aButton.tag == 222;
两个按钮的一种方法
- (void) methodToggle:(id) sender {
UIButton * btn = (UIButton *)sender;
if (btn.tag == 111)
{
UIButton * btnAnother = (UIButton *)[[btn superview] viewWithTag:222];
btnAnother.selected = NO;
}
if (btn.tag == 222)
{
UIButton * btnAnother = (UIButton *)[[btn superview] viewWithTag:111];
btnAnother.selected = NO;
}
btn.selected = YES;
}
答案 2 :(得分:0)
在tableView中:cellForRowAtIndexPath:添加以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
[cell.aButton setTag:1];
[cell.aButton addTarget:self action:@selector(methodA:) forControlEvents:UIControlEventTouchUpInside];
[cell.aButton setTag:2];
[cell.bButton addTarget:self action:@selector(methodB:) forControlEvents:UIControlEventTouchUpInside];
}
将按钮事件处理程序实现为 -
- (void) methodA:(id) sender
{
UIView *view = [sender superView];
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateSelected];
UIButton *otherButton = [view viewWithTag:2];
[otherButton setImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
}
- (void) methodB:(id) sender
{
UIView * view = [sender superView];
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];
UIButton *otherButton = [view viewWithTag:1];
[otherButton setImage:[UIImage imageNamed:@"default.png"] forState:UIControlStateNormal];
}
答案 3 :(得分:0)
希望这张图片能给你另一个提示。
要处理您的选择问题,最好的方法是在tableViewCell类中处理它,您可以在其中访问这两个按钮并根据需要编写委托或回调。
答案 4 :(得分:0)
试试这个,
- (void) methodA:(id) sender {
UIButton *buttonA = (UIButton *)sender;
CGPoint pointInTable = [field convertPoint:buttonA.bounds.origin toView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:nowIndex];
//now access the button and change button images
//cell.aButton setImage:
//cell.bButton setImage:
}
对按钮B操作执行相同的操作。
注意:我建议使用自定义tableview单元格并在自定义单元格内处理此按钮操作。
答案 5 :(得分:0)
编写一个CustomCell类,它有2个带有目标方法的按钮和委托方法,用于在单击按钮时通知表视图。在表格视图中使用此单元格。
在目标方法中,图像更改(A / B)并触发委托方法以通知tableViewController。
您可以将indexpath作为单元格的属性保存,并将其传递给tableView的委托。(在tableView的cellForRowAtIndexPath中设置单元格的索引路径:)然后您可以识别单击了哪个单元格的按钮和相关的数据对象可以从您的数据源中获取。
CustomCell.m
- (IBAction)onButtonAClicked:(id)sender {
// change your images
[delegate cellButtonClicked:self.indexpath];
}
TableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.delegate = self;
}
cell.indexpath = indexPath;
}
//delegate method
-(void)cellButtonClicked:(NSIndexPath *)indexPath{
//get the related data object for the cell using indexPath from your data source
}