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!
Set own cell accessory type
Asked Answered
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
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) {
}
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"? –
Utility
@Utility 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: #32351518 –
Therapsid
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. –
Utility
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
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
}
I don't think the last part works -> "Use this method to respond to taps in the detail button accessory view of a row. The table view does not call this method for other types of accessory views." –
Firecure
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)
{
}
© 2022 - 2024 — McMap. All rights reserved.