我想在 tableview 单元格上的按钮上添加图像,这是我的代码,一切正常,但在选择图像后,它未设置或在 imagebutton
上不可见,请帮助我。 .....
在 cellForRowAtIndexpath
方法中
if(indexPath.row == 0)
{
cell.userInteractionEnabled = YES;
cell.textLabel.text =@"Your Profile Photo";
imageBtn = [[UIButton alloc]initWithFrame:CGRectMake(250, 5, 35, 35)];
[imageBtn setBackgroundImage:[UIImage imageNamed:@"AFPFriendThumbnail.png"] forState:UIControlStateNormal];
[imageBtn addTarget:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
imageView.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:imageBtn];
}
按钮上的 Action
-(void)myAction
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
[imageBtn setImage:image forState:UIControlStateSelected];
imageBtn.backgroundColor = [UIColor redColor];
[tableView_ reloadData];
}
最佳答案
尝试使用 UIControlStateNormal
[imageBtn setImage:image forState: UIControlStateNormal];
但我可以看到您在设置图像后重新加载 tableView,这将替换 imageBtn
imageBtn = [[UIButton alloc]initWithFrame:CGRectMake(250, 5, 35, 35)]; // Creating a button every time.
它应该无需重新加载即可工作,因为您保留了对 imageBtn
的引用,以便您可以直接设置。尝试删除 [tableView_ reloadData]
关于ios - 在 tableview 上添加图像自定义按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27483250/