Cursor not appearing in UITextView iOS - ios

I have an expandable tableViewCell with a couple UITextFields, a UILabel and a UITextView. When the tableView expands the label disappears and the UITextFields and UITextView appear. In both UITextFields the cursor appears but in the UITextView no cursor appears. I have tried changing the tint color and nothing shoed up, however when I select text (which does appear) the text is highlighted in the tint color and is visible. I have also tried changing the frame but that didn't work. Also I have it setup now as a regular UITextView with no changes to any properties other than the frame. The textView is setup programmatically using a UITableViewCell class and everything seems to work but the cursor.
Here's how I set up the UITextView:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! StudentTableViewCell
cell.infoView.frame = CGRect(x: 23, y: 207, width: cell.frame.width - 36, height: cell.frame.height - 155)
cell.infoView.delegate = self
cell.infoView.text = classes[indexPath.row].info
cell.infoView.layer.cornerRadius = 5
cell.infoView.layer.masksToBounds = true
cell.placeholderField.frame = CGRect(x: 32, y: 92, width: self.view.frame.width - 38, height: 45)
if cell.infoView.text!.replacingOccurrences(of: " ", with: "") == "" {
cell.placeholderField.placeholder = "Description"
} else {
cell.placeholderField.placeholder = ""
}
cell.placeholderField.backgroundColor = UIColor.clear
cell.nameField.frame = CGRect(x: 23, y: 3, width: cell.frame.width - 70, height: 44)
cell.nameField.text = classes[indexPath.row].name
cell.nameField.delegate = self
cell.nameField.layer.cornerRadius = 5
cell.nameField.layer.masksToBounds = true
cell.nameField.borderStyle = .roundedRect
cell.teacherField.frame = CGRect(x: 23, y: 50, width: cell.frame.width - 36, height: 44)
cell.teacherField.text = classes[indexPath.row].teacher.name
cell.teacherField.delegate = self
cell.teacherField.layer.cornerRadius = 5
cell.teacherField.layer.masksToBounds = true
cell.teacherField.borderStyle = .roundedRect
cell.editButton.frame = CGRect(x: self.view.frame.width - 60, y: 15, width: 70, height: 20)
cell.editButton.addTarget(self, action: #selector(self.editInfoView), for: .touchUpInside)
cell.editButton.setTitle(cell.editButton.title(for: .normal) ?? "Edit", for: .normal)
cell.contentView.addSubview(cell.nameField)
cell.contentView.addSubview(cell.teacherField)
cell.contentView.addSubview(cell.infoView)
cell.contentView.addSubview(cell.editButton)
cell.contentView.addSubview(cell.placeholderField)
cell.textLabel?.text = classes[indexPath.row].name
cell.detailTextLabel?.text = classes[indexPath.row].teacher.name
return cell
}
Here is the Class definition:
class StudentTableViewCell: UITableViewCell {
var infoView = UITextView()
var placeholderField = UITextField()
var nameField = UITextField()
var teacherField = UITextField()
var editButton = UIButton()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Does anyone know why the cursor isn't appearing?
Thanks in advance for the help.
Here are photos from the debug hierarchy with the cursor selected:
Photo from the side
Photo from the front
The view in front of the cursor is the placeholderField and is not blocking the cursor because the cursor still doesn't appear when it is below the placeholderField.

First of all, I don't know how's your code is working for you as without compiling it I can see few major issues like.
Where you are returning the Cell in cellForRowAt indexPath?
cell.teacherNameField is not there in StudentTableViewCell
And frankly speaking, these things are not difficult they way you are trying to code, Just keep it simple and think before writing code (each and every possible approach) then only you will achieve the best practice.
Now coming back to your question, I tried with your codebase and thing are getting overlap that's you are not able to do this, so you are the best person who can solve it.
Use Debug View Hierarchy
And then you can see each and every runtime view layer on your screen.

Related

Swift 5 - UIButtons Side by side in tableView footer with original separator line

I am trying to add two buttons programmatically side by side (on the bottom left) in the tableView footer.
The issue that I am having is that I have to manually draw the separator line when defining the tableView footer because the separator line disappears.
How can I simply add two buttons to the bottom left of the tableView footer without loosing the original separator line?
var terms_button = UIButton()
var policy_button = UIButton()
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//terms button
terms_button = UIButton(frame: CGRect(x: 70, y: 0, width: 100, height: 50))
terms_button.setTitle("Terms", for: .normal)
terms_button.setTitleColor(UIColor.black, for: .normal)
terms_button.titleLabel?.font = UIFont.roboto(size: 12, weight: .medium)
terms_button.titleLabel?.alpha = 0.38
terms_button.addTarget(self,action: #selector(didTapTermsButton),for: .touchUpInside)
//policy button
policy_button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
policy_button.setTitle("Privacy Policy", for: .normal)
policy_button.setTitleColor(UIColor.black, for: .normal)
policy_button.titleLabel?.font = UIFont.roboto(size: 12, weight: .medium)
policy_button.titleLabel?.alpha = 0.38
policy_button.addTarget(self,action: #selector(didTapPolicyButton),for: .touchUpInside)
let result = UIView()
// recreate insets from existing ones in the table view
let insets = tableView.separatorInset
let width = tableView.bounds.width - insets.left - insets.right
let sepFrame = CGRect(x: insets.left, y: -0.5, width: width, height: 0.5)
// create layer with separator, setting color
let sep = CALayer()
sep.frame = sepFrame
sep.backgroundColor = tableView.separatorColor?.cgColor
result.layer.addSublayer(sep)
result.addSubview(policy_button)
result.addSubview(terms_button)
return result
}
When you are returning your own let result = UIView() view instance from viewForFooterInSection, you are discarding the original built-in default view provided by the iOS.
What you can attempt is -
delete the viewForFooterInSection implementation
try to use default built-in view provided by iOS
try to customize default view's appearance like following
func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
guard let footerView = view as? UITableViewHeaderFooterView else { return }
let contentView = footerView.contentView
// Try adding your buttons to this `contentView`
}
This is the only way to try to keep using built-in view with possible customizations. If this doesn't work reliably across different iOS versions, you would need to go back to viewForFooterInSection custom view implementation.

Swift - Add UIRefreshControl to section header view of table

I am trying to add a UIRefresh control to the section header table depending on a flag, however it is not showing up, can somebody see the reason?
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
...
let textAttribs = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name: "Arial", size: 16.0)!]
if appsAreGettingInstalled {
let titleString = NSAttributedString(string: "Apps are getting installed", attributes: textAttribs)
let lbl = UILabel(frame: CGRect(x: 10, y: 0, width: tableView.bounds.size.width - 20, height: 40))
lbl.attributedText = titleString
let refreshCtrl = UIRefreshControl()
refreshCtrl.beginRefreshing()
let view = UIScrollView(frame: CGRect(x: 10, y: 50, width: tableView.bounds.size.width - 20, height: 40))
view.addSubview(refreshCtrl)
view.addSubview(lbl)
headerView.addSubview(view)
}
...
}
I also tried view.refreshControl = refreshCtrl but it's still not shown, only the label.
There is also a simple way of adding refresh control through Interface builder.
First, select the tableViewController in your storyboard, open the attribute inspector, and enable refreshing
Now in your viewdidLoad add following code:
self.refreshControl?.addTarget(self, action: #selector(ViewController.refreshLogic(_:)), for: UIControlEvents.valueChanged)
Finally, refreshLogic function may look like following:
func refreshLogic(refreshControl: UIRefreshControl) {
// Insert your logic here do some reloading of data and update the table view's data source
// reload your table
self.yourTableViewObj.reloadData
refreshControl.endRefreshing()
}
I think this can also work for you. Happy Coding
As trungduc suggested, using an activity indicator view instead of the refresh control solved my issue - thanks

Swift 3 add remove subview and its content dynamically

I am writing my first Swift app for quiz. Each question has random way to render on screen as below screenshot.
I am programming app without story board ie. programmatically. I want to create simple pagination flow for each question within single viewcontroller without using Collocationview, Tableview or navigation.
What I did so far? I have simple viewcontroller with UIView() added as subview. I am adding question components dynamically. Now once user click on continue I wanted remove subview and add new subview with new question. I am able to remove subview but contents on subview seems to be still there as I can see its overwriting.
To get more clarification please view my code.
import UIKit
class QuizController: UIViewController {
let subView = UIView()
var currentQuestion:Int = 1;
let questions = ["This is question 1", "Hello to question 2", "Question 3"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
setup_layout()
}
func setup_layout(){
let closeBtn = UIButton(frame: CGRect(x: 5, y: 10, width: 200, height: 50))
closeBtn.backgroundColor = UIColor.red
closeBtn.setTitle("Close", for: .normal)
closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
view.addSubview(closeBtn)
//dynamic view
create_subview()
}
func nextQuestion(){
print("Show next")
if let viewWithTag = self.view.viewWithTag(currentQuestion) {
viewWithTag.removeFromSuperview()
currentQuestion += 1;
create_subview()
} else {
print("No!")
}
}
func create_subview(){
let heightOfView = view.frame.size.height
let widthOfView = view.frame.size.width
subView.tag = currentQuestion
subView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)
subView.frame = CGRect(x: 0, y: 60, width: self.view.frame.width, height: heightOfView - 60)
self.view.addSubview(subView)
let txtLabel1 = UILabel(frame: CGRect(x: 35, y: 120, width: widthOfView , height: 20))
txtLabel1.text = questions[currentQuestion-1]
txtLabel1.font = txtLabel1.font.withSize(12)
subView.addSubview(txtLabel1)
let nextBtn = UIButton(frame: CGRect(x: 5, y: 300, width: 200, height: 50))
nextBtn.backgroundColor = UIColor.red
nextBtn.setTitle("Continue", for: .normal)
nextBtn.addTarget(self, action: #selector(nextQuestion), for: .touchUpInside)
subView.addSubview(nextBtn)
}
func close(){
self.dismiss(animated: true, completion: nil)
}
}
And this is what I see which I click continue.
NOTE: Initially thought using Collocation or Table view will be more appropriate as I can set to scroll horizontally for each question and fetch questions using REST API and place to each cell. But I want to present next screen to user only once then click on continue. I guess with collectionview user can move to next screen on swipe.
Just found the answer. I assumed removing subview will also remove all components on subview by itself ie. UILable and UIButton in my case.
I have to remove them separately.
for subview in self.subView.subviews {
subview.removeFromSuperview()
}
Now i can add tag to components and remove like this:
for subview in self.subView.subviews {
if (subview.tag == 1) {
subview.removeFromSuperview()
}
}
Why are you removing superviews and adding again and again??
Simply change UILabel.text :)

how can I display the following scenes hierarchy in swift using collectionView?

I have the following display scenes available. I am getting confused what type of hierarchy of controls I should take to display these type of view in xib .
please give ideas to show these types of scenes. because my items are coming dynamically . Its not fixed. so if I took tableview to display the first items and its categories then where should i display the rest items.
Edited
I took four sections. In 1st section collection and delivery buttons. In 3rd notes and in 4th allergy & checkout .
In 2nd my order items are there. but here I have two level of data.. order item name like chicken kabab small,... etc and 2nd level its addons like plain nan, bottle of drink,... etc. Here my order items is iterating in cell as well as my addons are iterating. I took the order items name in cell. now where should i take the addon items programatically and how to set the size of each cell based on its all contents inside it.
class cartVC: UIViewController ,UITableViewDataSource,UITableViewDelegate,UITextViewDelegate{
var tableData = ["al","dbd","gdge","kjdkas","al","dbd","gdge","kjdkas","al","dbd","gdge","kjdkas","al","dbd","gdge","kjdkas"]
var mainview = UIView()
#IBOutlet weak var cartTableView: UITableView!
#IBAction func backBtn(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func changeColor(sender:UISegmentedControl){
switch(sender.selectedSegmentIndex){
case 0:
print("collection clicked")
case 1:
print("delivery clicked")
default:
self.view.backgroundColor = UIColor.blueColor()
}
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 4
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowcount = 0
if section == 0{
rowcount = 0
}
if section == 1 {
rowcount = tableData.count
}
if section == 2{
rowcount == 0
}
if section == 3{
rowcount == 0
}
return rowcount
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0{
let headerView = UIView()
//set the frame
let frame = UIScreen.mainScreen().bounds
// headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 60)
headerView.frame = CGRectMake(frame.minX , frame.minY, frame.width, 60)
headerView.backgroundColor = UIColor.whiteColor()
//Initialize segment control
let items = ["Collection","Delivery"]
let customSC = UISegmentedControl(items: items)
customSC.selectedSegmentIndex = 0
//set the frame amd segmented control
customSC.frame = CGRectMake(frame.minX + 10, frame.minY + 5, frame.width - 20, 30)
// style the segmented control
customSC.layer.cornerRadius = 5.0
customSC.backgroundColor = UIColor.clearColor()
customSC.tintColor = UIColor.redColor()
//add target action method
customSC.addTarget(self, action: #selector(CartViewController.changeColor(_:)), forControlEvents: .ValueChanged)
//add subview
headerView.addSubview(customSC)
//Add label
let headinglbl = UILabel(frame: CGRect(x: frame.minX + 10, y: frame.minY + 40, width: tableView.frame.size.width, height: 20))
headinglbl.text = "Your Order"
headinglbl.font = UIFont.boldSystemFontOfSize(17)
headinglbl.textColor = UIColor.blackColor()
headinglbl.textAlignment = .Center
headerView.addSubview(headinglbl)
mainview = headerView
}
if section == 2{
let totalView = UIView()
totalView.frame = CGRectMake(0, 0, tableView.frame.size.width, 60)
totalView.backgroundColor = UIColor.clearColor()
//Add discount label
let discount = 14.5
let discountlbl = UILabel(frame: CGRectMake(10, 0, tableView.frame.size.width, 20))
discountlbl.text = "Online Collection Discount(\(discount)%)"
discountlbl.font = UIFont.systemFontOfSize(14)
discountlbl.textColor = UIColor.darkGrayColor()
discountlbl.textAlignment = .Left
totalView.addSubview(discountlbl)
//Add discount price
let discountprice = UILabel(frame: CGRectMake(tableView.frame.size.width-60, 0, tableView.frame.size.width, 20))
discountprice.text = "£ 1.27"
discountprice.font = UIFont.systemFontOfSize(14)
discountprice.textColor = UIColor.blackColor()
discountprice.textAlignment = .Left
totalView.addSubview(discountprice)
//Add label
let lbl = UILabel(frame: CGRectMake(10, 20, tableView.frame.size.width, 40))
lbl.text = "Total"
lbl.font = UIFont.boldSystemFontOfSize(20)
lbl.textColor = UIColor.blackColor()
lbl.textAlignment = .Left
totalView.addSubview(lbl)
//calculate amount label
let totalAmountLbl = UILabel(frame: CGRectMake(totalView.frame.width-70, 20, totalView.frame.width, 40))
totalAmountLbl.text = "£ 0.0"
totalAmountLbl.font = UIFont.boldSystemFontOfSize(20)
totalAmountLbl.textColor = UIColor.blackColor()
totalAmountLbl.textAlignment = .Left
totalView.addSubview(totalAmountLbl)
mainview = totalView
}
if section == 3{
let footerView = UIView()
footerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 200)
footerView.backgroundColor = UIColor.clearColor()
//Add note label
let notelbl = UILabel(frame: CGRectMake(10, 10, tableView.frame.size.width, 20))
notelbl.text = "Leave a note"
notelbl.font = UIFont.boldSystemFontOfSize(17)
notelbl.textColor = UIColor.blackColor()
notelbl.textAlignment = .Left
footerView.addSubview(notelbl)
//Add a note textview
let noteTxt = UITextView()
noteTxt.frame = CGRectMake(10, 40, footerView.frame.width-20, 50)
noteTxt.backgroundColor = UIColor.lightGrayColor()
noteTxt.keyboardType = UIKeyboardType.Default
noteTxt.text = "e.g. Instructions about yout order"
noteTxt.textColor = UIColor.blackColor()
noteTxt.delegate = self
footerView.addSubview(noteTxt)
// Add allergy button
let allergyBtn = UIButton(type:.System)
allergyBtn.frame = CGRectMake(50, 100, 200, 20)
allergyBtn.setTitle("Do You have any allergy ?", forState: .Normal)
allergyBtn.setTitleColor(UIColor.redColor(), forState: .Normal)
allergyBtn.titleLabel?.font = UIFont(name: "", size: 10)
footerView.addSubview(allergyBtn)
// Add checkout button
let checkoutBtn = UIButton(type:.System)
checkoutBtn.frame = CGRectMake(100, 140, 100, 40)
checkoutBtn.setTitle("Check out", forState: .Normal)
checkoutBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
checkoutBtn.titleLabel?.font = UIFont(name: "", size: 10)
checkoutBtn.backgroundColor = UIColor.redColor()
checkoutBtn.layer.cornerRadius = 5
footerView.addSubview(checkoutBtn)
mainview = footerView
}
return mainview
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cartcell")! as! CartTableViewCell
cell.itemLabel.text = tableData[indexPath.row]
return cell
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
var heightCount:CGFloat = 0
if section == 0{
heightCount = 60.0
}
if section == 2{
heightCount = 60.0
}
if section == 3{
heightCount = 200.0
}
return heightCount
}
My customcell code
import UIKit
class CartTableViewCell: UITableViewCell {
let padding: CGFloat = 5
var background: UIView!
var itemLabel: UILabel!
var priceLabel: UILabel!
var deleteBtn:UIButton!
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = UIColor.clearColor()
selectionStyle = .None
background = UIView(frame: CGRectZero)
background.alpha = 0.6
contentView.addSubview(background)
deleteBtn = UIButton(frame: CGRectZero)
deleteBtn.setImage(UIImage(named: "deleteBin.png"), forState: .Normal)
contentView.addSubview(deleteBtn)
itemLabel = UILabel(frame: CGRectZero)
itemLabel.textAlignment = .Left
itemLabel.textColor = UIColor.whiteColor()
contentView.addSubview(itemLabel)
priceLabel = UILabel(frame: CGRectZero)
priceLabel.textAlignment = .Center
priceLabel.textColor = UIColor.whiteColor()
contentView.addSubview(priceLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
background.frame = CGRectMake(0, padding, frame.width, frame.height-2 * padding)
deleteBtn.frame = CGRectMake(padding, (frame.height - 25)/2, 40, 25)
priceLabel.frame = CGRectMake(frame.width-100, padding, 100, frame.height - 2 * padding)
itemLabel.frame = CGRectMake(CGRectGetMaxX(deleteBtn.frame) + 10, 0, frame.width - priceLabel.frame.width - CGRectGetMaxX(deleteBtn.frame) + 10, frame.height)
}
}
As our mates already said about using tableview and sections, Here we gonna follow the same way.Since it is a broad topic to explain i'll give some hint and at last you can find link for demo project.
First add a tableview in your storyboard then add collection,Delivery & Your order objects as tableview header
Create a new class subclass of UITableviewcell with xib let's name it as Cell1.Now add delete icon, main dish label and price label,for sub items we gonna use another UITableview.
Now create another UITableviewcell with xib name it as Cell2, prepare that xib for sub items and their price.
In cell1 numberOfSectionsInTableView return number of main dish count and in numberOfRowsInSection return 1, Now load name of all main dishes in their respective label's
Upto now we having some number of section(depending on number of main items) each section having one UITableview.
Now we have to change height of tableview cell dynamically depending on SubItems count. so in heightForRowAtIndexPath i have added following lines
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let subItems = tableContainer![indexPath.section].valueForKey("additional_items") as! NSArray
var defaultCellHeight:CGFloat = 37//Consider 37 as height of cell without subitems
//Following for loop keeps on increasing defaultCellHeight depending on available count
for _ in subItems {
defaultCellHeight += 37
}
return defaultCellHeight + 20
}
Since it is hard to explain everything deeply i have provide code for heightForRowAtIndexPath.While looking into the demo project you'll understand everything
NOTE : Upto now we have loaded all main dishes details, and we have provided enough room for upcoming sub item's.
In Cell1 class add tableview delegate and datasource in awakeFromNib,add all datasource methods as required.set numberOfSectionsInTableView as 1 and numberOfRowsInSection as subitem count
That's it we have loaded tableview dynamically as per your requirement.
Now at last add discount, total, leave a note& Checkout objects in separate tableviewcell class an load it at last index.
Or add add all those objects inside a UIView and add it as Main tableview's footer.
NOTE : The above hints are just for reference, For better clarification of concept i have added a demo project's github repo.
RESULT :

Set own cell accessory type

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!
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)
}
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
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)
{
}
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
}

Resources