我被困在一个地方,我想在一个图像按钮上添加一个图像,这样当我点击它时,按钮的图像就会改变。我必须在didSelectRow方法中编写逻辑。因此,我需要编写以下代码行来添加该图像:
button=[UIButton alloc] initWithFrame:CGRectMake(230,0,40,40];
button.addTarget: self action: @selector(buttonPressed:withEvent:) forControlEvents:UIControlEventTouchUpInside];
button.tag=indexPath.row;
[button setImage: [UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
[cell addSubview: button];
虽然这在我用cellForRow编写上述代码时可以用,但在didSelectRow中不能用,因为单元格不是在这个方法中定义的。
发布于 2012-10-01 09:33:13
尝尝这个
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
for (UIButton *button in cell.subviews) {
if (button.tag==indexPath.row) {
[button setImage: [UIImage imageNamed:@"another.png"] forState:UIControlStateNormal];
}
}
}
发布于 2012-10-01 09:27:03
在didSelectRow中尝试此操作以获取单元格
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
https://stackoverflow.com/questions/12670566
复制