7

I'd like to set my own cellAccessoryType (an image) in an UITableViewCell. Do you know how I can do this? I'm using Swift, Xcode 6.2 and iOS 8.2. Thank you for you help!

Vpor
  • 147
  • 1
  • 1
  • 7

6 Answers6

16

Try this:

// first create UIImageView
var imageView : UIImageView
imageView  = UIImageView(frame:CGRectMake(20, 20, 100, 320))
imageView.image = UIImage(named:"image.jpg")

// then set it as cellAccessoryType
cell.accessoryView = imageView

PS: I strongly advise you to upgrade to XCode 6.3.2 and using iOS 8.3 SDK

Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
10

I assume you would like to get tap on accessory view, so I provide this answer with button. If not, use shargath's answer with imageView.

var saveButton = UIButton.buttonWithType(.Custom) as UIButton
        saveButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        saveButton.addTarget(self, action: "accessoryButtonTapped:", forControlEvents: .TouchUpInside)
        saveButton.setImage(UIImage(named: "check-circle"), forState: .Normal)
        cell.accessoryView = saveButton as UIView

func accessoryButtonTapped(sender:UIButton) {

}
Shmidt
  • 16,436
  • 18
  • 88
  • 136
3

Swift 5 version of Shmidt's answer, plus using sender.tag to keep track of which row the button was clicked on:

override func tableView(_ tableView: UITableView, 
    cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", 
        for: indexPath)
    let checkButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    checkButton.addTarget(self, action:#selector(YOUR_CLASS_NAME.checkTapped(_:)), 
        for: .touchUpInside)
    checkButton.setImage(UIImage(named: "check"), for: .normal)
    checkButton.tag = indexPath.row
    cell.accessoryView = checkButton
    return cell
}


@objc func checkTapped(_ sender: UIButton) {
    print(sender.tag)
}
  • Would there be any possibility to send the "indexPath.section" and the "indexPath.row"? – Markus Nov 15 '20 at 18:47
  • 1
    @Markus Yes, there's a few different ways of doing this, but an easy one is just to combine section and row into one number, like 1018 (section 10, row 18) and assign that to tag. More info and other ways: https://stackoverflow.com/questions/32351018/send-row-and-section-through-tag-in-button-swift – Roman Sheydvasser Nov 16 '20 at 19:31
  • Thank you. Very interesting. At first sight I thought to merge 2 numbers into 1 (section+row) but I don't know of how to retrieve the values, because the section and row can have 1 digit, 2 digits, etc. I did not know either create a subclass of UIButton with an extra property: IndexPath. – Markus Nov 17 '20 at 14:23
3

Swift 5 version, adding contentMode:

    let imageView: UIImageView = UIImageView(frame:CGRect(x: 0, y: 0, width: 20, height: 20))
    imageView.image = UIImage(named:Imge.Card.Link.download)
    imageView.contentMode = .scaleAspectFit
    cell.accessoryView = imageView
Claytog
  • 618
  • 8
  • 8
1

SWIFT 5 solution

This example presents how to add simple button with SF Symbols to your accessory view.

inside cellForRowAt:

let plusButton = UIButton(type: .system)
plusButton.setImage(UIImage(systemName: "plus"), for: .normal)
plusButton.contentMode = .scaleAspectFit
plusButton.sizeToFit()
cell.accessoryView = plusButton

and instead of adding target to the button we can use delegate method from UITableViewDelegate

func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
    //handle logic here
}
ShadeToD
  • 403
  • 9
  • 22
0

Xamarin.iOS version of Shmidt's answer

// Create the accessory
var accessory = new UIButton(UIButtonType.Custom);
accessory.Frame = new CGRect(0f, 0f, 24f, 24f);
accessory.AddTarget(accessoryButtonTapped, UIControlEvent.TouchUpInside);
accessory.SetImage(UIImage.FromBundle("check-circle"), UIControlState.Normal);
// Set the accessory to the cell
cell.AccessoryView = accessory as UIView;

void accessoryButtonTapped(object sender, EventArgs args)
{

}
stepheaw
  • 1,683
  • 3
  • 22
  • 35