Вопрос

I've got a TableViewController with a couple custom table cells. I was just playing around with it and I did a bit of scrolling. I noticed when I scroll a TableViewCell outside the View and it reappears that the placeholder text got a bit darker. I think it is because the TableViewCell is reloaded and there are now 2 placeholders on top of each other. I also got a custom table cell with a Switch. When that cell is out of the view and got back in, it takes back the original value (when I put the switch 'off' and scroll it out of the view, it comes back as 'on').

Anyone had this problem before? Why are the TableViewCells reloading when they appear in the View again?

PS. I create the TableViewCells by deserializing a Json file, that is done within the ViewDidLoad() method.

This is my GetCell method:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        if (element [indexPath.Row].Type == "textField") {

            NSString FieldID = new NSString ("EditField");

            UITableViewCell cell = tableView.DequeueReusableCell (FieldID);

            //Code to fill the textfield table cell

            return cell;
        } 
        if (element [indexPath.Row].Type == "switch") {

            NSString FieldID = new NSString ("SwitchField");

            UITableViewCell cell = tableView.DequeueReusableCell (FieldID);

            //Code to fill switch table cell

            return cell;
        }
    }
Это было полезно?

Решение

If someone has the same problem, I had to check if the TableCell elements already had a property. To check the elements I worked with Tags.

This is the code I used to check if TextField already was set or not:

var setTextField = cell.ViewWithTag (99) as UITextField;

if (setTextField == null) {
    TextField = new UITextField ();
    TextField.Placeholder = element [indexPath.Row].Placeholder;

    cell.AddSubview (TextField);
} else {
    //Nothing
}

Другие советы

Without seeing your code it's impossible to say for sure, but the default behavior of UITableViewCell (if you follow the examples provided by Apple and Xamarin) is to reuse cells instead of recreating them every time. This means that as a cell scrolls off of the screen, it is cached and then will be reused when a new cell is needed. You cannot assume that the new cell you are provided is "blank" - you will need to reinitialize it yourself.

If you have multiple types of cells, you should differentiate then with unique CellIdentitifier keys. This is what tells the caching mechanism which type of cell needs to be retrieved.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top