Reputation: 1469
If you use a custom cell accessory instead of the DetailDisclosureButton you can no long use the AccessoryButtonTapped event...how do I use a custom cell accessory (i.e. a UIButton set as the AccessoryView for the cell) and be able to get the NSIndexPath that countains the row and section that was selected?
image = UIImage.FromFile ("Images/checkmark.png");
lockButton = new UIButton(UIButtonType.Custom);
frame = new RectangleF(0f, 0f, 25f, 25f);
lockButton.Frame = frame;
lockButton.SetBackgroundImage(image, UIControlState.Normal);
lockButton.TouchUpInside += (sender, e) =>
{
try{
int i =0;
foreach(UIView sv in cell.Subviews)
{
if(cell.Subviews[i] is UITableView)
;
}
}
catch
{
}
};
if (sourceType == TableSoureType.Collections && collGroup [indexPath.Section].LocationEntity [indexPath.Row].CollectionStatus.ToLower () != "open") {
cell.TextLabel.TextColor = UIColor.Gray;
cell.AccessoryView = lockButton;
}
else if(sourceType == TableSoureType.Collections && collGroup [indexPath.Section].LocationEntity [indexPath.Row].CollectedToday) {
cell.TextLabel.TextColor = UIColor.Black;
cell.AccessoryView = lockButton;
}
else
{
cell.TextLabel.TextColor = UIColor.Black;
cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;
}
This may be cause for a new post but I also have an issue where when I click the button the application exits on the simulator - I understand that it may be a garbage collection issue so I moved the declaration of my button, image, and frame to the class level to no avail.
So I have two issues:
1 - Once I get the TouchUpInside event to work correctly how do I figure out which cell was selected when that event was fired?
2 - How to get the TouchUpInside event to fire without causing a crash on the simulator - from my understanding its a garbage collection issues - i guess its also important to not that the crash doesn't happen on the device itself...but the code thats in the inside doesn't work either
Upvotes: 0
Views: 476
Reputation: 1985
If I understand correctly you're putting a UIButton inside your cells, and want to use that instead of the RowSelected event of the UITableView?
To solve the memory issues, add the UIButtons to a List before adding them to your cells. You can set the Tag property on the buttons to track them.
Somewhere that will persist while the UITableView is present..
List<UIButton> buttons = new List<UIButton> ();
In your function that created the UITableView
UIButton b = new UIButton (new RectangleF(140, 10, 385, 40));
b.Tag = buttons.Count;
buttons.Add (b);
UIButton b2 = new UIButton (new RectangleF(140, 10, 385, 40));
b2.Tag = buttons.Count;
buttons.Add (b2);
To get the cell on button click do something like this:
NSIndexPath ns = NSIndexPath.FromRowSection (b.Tag, 0);
UITableViewCell tcell = tableView.CellAt (ns);
Upvotes: 1