ios - 在 tableview 上添加图像自定义按钮背景

标签 ios objective-c uitableview

我想在 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/

相关文章:

ios - 带 2 个导航栏的导航 Controller - 如何调整推送 View Controller 的框架?

ios - 使用自动布局获取自定义 UITableViewCell 内 UIButton 的大小

ios - UITableViewCell 和标签不显示文本

ios - 如何在Xcode中从 'first throw call stack'获取信息

ios - TransitionTo 上的 KineticJS 颠簸旋转

ios - 为什么-frameForAlignmentRect:和-alignmentRectForFrame:不调用IOS

ios - 应用程序关闭后不保存用户默认值

iOS 自动补全不适用于 RxCocoa Xcode 8.3

ios - FBSDK 和 LinkingManager 的 AppDelegate.m

ios - 单击 tableview 中的 collectionview 单元格导航