Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS TableView button is being called multiple times

I have a TableView with a custom UITableViewCell. In each cell I have multiple buttons, when any of the buttons is click after scrolling down and up it calls itself the many times I scroll down and up.

I have read and research for a solution and I haven't found a solution.

I know the problem is the cell are being reuse so that's why the buttons are being call multiple times but I can't find a way to prevent it.

I added console write line statements through the code and the else part in MoveToWindow is never call. Could that be the reason why?

Research material for solution:

my code is calling twice the btndelete method in uitableview

UIButton click event getting called multiple times inside custom UITableViewCell

My Code:

namespace Class.iOS
{
    public partial class CustomCell : UITableViewCell
    {
        public static readonly NSString Key = new NSString ("CustomCell");
        public static readonly UINib Nib;
        public int Row { get; set; }
        public event EventHandler LikeButtonPressed;

        private void OnLikeButtonPressed()
        {
            if (LikeButtonPressed != null)
            {
                LikeButtonPressed(this, EventArgs.Empty);
            }
        }

        public override void MovedToWindow()
        {
            if (Window != null)
            {
                btnAdd.TouchUpInside += HandleLikeButtonPressed;
            }
            else
            {
                btnAdd.TouchUpInside -= HandleLikeButtonPressed;
            }
        }

        private void HandleLikeButtonPressed(object sender, EventArgs e)
        {
            OnLikeButtonPressed();
        }

        static CustomCell ()
        {
            Nib = UINib.FromName ("CustomCell", NSBundle.MainBundle);
        }

        public CustomCell ()
        {           
        }

        public CustomCell (IntPtr handle) : base (handle)
        {           
        }

        public void UpdateCell (string Name, int number)
        {
            // some code
        }

        public class TableSource : UITableViewSource
        {           
            public override nint RowsInSection (UITableView tableview, nint section)
            {
                return 8;
            }

            private void HandleLikeButtonPressed(object sender, EventArgs e)
            {
                var cell = (CustomCell)sender;
                var row = cell.Row;

                switch (row) 
                {
                case 0:
                    cell.label.Text = ""
                    break;
                case 1:
                    cell.label.Text = ""
                    break;
                }
            }
            public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                var cell = tableView.DequeueReusableCell (CustomCell.Key) as CustomCell;
                cell.Row = indexPath.Row;

                if (cell == null) 
                {
                    cell = new CustomCell ();
                    var views = NSBundle.MainBundle.LoadNib("CustomCell", cell, null);
                    cell.LikeButtonPressed += HandleLikeButtonPressed;
                    cell = Runtime.GetNSObject( views.ValueAt(0) ) as CustomCell;

                }

                cell.UpdateCell 
                (
                    // some code
                );

                return cell;
            }
        }
    }
}
like image 947
PLOW Avatar asked Mar 11 '16 06:03

PLOW


1 Answers

The cells are reused in iOS, so you need to make sure you properly unhook handlers and reset state when a cell is reused. You can do something like this:

public partial class CustomCell : UITableViewCell {

EventHandler _likeButtonHandler;

public static readonly NSString Key = new NSString (typeof(CustomCell).Name);
public static readonly UINib Nib = UINib.FromName (typeof(CustomCell).Name, NSBundle.MainBundle);

public CustomCell ()
{

}

public CustomCell (IntPtr handle) : base (handle)
{
}

public override void PrepareForReuse ()
{
    LikeButton.TouchUpInside -= _likeButtonHandler;
    _likeButtonHandler = null;

    base.PrepareForReuse ();
}

public void SetupCell (int row, string Name, EventHandler likeBtnHandler)
{
    _likeButtonHandler = likeBtnHandler;

    LikeButton.TouchUpInside += _likeButtonHandler;
    LikeButton.Tag = row;

    NameLabel.Text = Name;
    RowLabel.Text = row.ToString ();
}

Notice that I unhook the event handler in the PrepareForReuse override. This is the correct place to do clean up and reset a cell for reuse. You should NOT be using MovedToWindow().

Then your GetCell method will look like this:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    var cell = tableView.DequeueReusableCell (CustomCell.Key) as CustomCell ?? new CustomCell ();

    cell.SetupCell (indexPath.Row, _fruits [indexPath.Row], _likeButtonHandler);

    return cell;
}

The _likeButtonHandler is a simple EventHandler.

like image 129
pnavk Avatar answered Nov 09 '22 20:11

pnavk