Forum Xamarin.iOS

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

App freeze without log when scroll in UITableView and the

lufo88lufo88 USMember ✭✭✭

Hi,

I developed an app with Xamarin.iOS three years ago and with the release of iOS 14 I decide to check bugs or unwanted behavior.

I got this error: if I start to scroll in a view with a tableView inside, the app freeze, without error or warnings (I check also the device log).
After two hour of test I identity the problem in the code I use to populate the field of my custom UITableViewCell.

In this method I set 3 UIlabel and two UIImage.
The set of the two UIImage works, but I can't set the text in the UILabel if the text come from an object.
In other word, this works:
NameLabel.Text = "Text foo";
but this not
NameLabel.Text = !string.IsNullOrWhiteSpace(obj.text) ? obj.text : "N/A" ;

As you can see I check the text exist and it is not empty (the obj is never null, othrwise it will crash in a previous point). Also that text is about 8 or 10 characters. No more.

It's a really strange behavior. Someone have a clue about it?

Best Answer

  • lufo88lufo88 USMember ✭✭✭
    Accepted Answer

    In the end the problem is a reference to a subclass of UILabel I use in my project. In the xib one of the label isn't of UiLabel but of my custom subclass of UILabel.

    I don't know why this cause a problem (I use the subclass in other View of the app without warnings or errors), but remove this reference (now it is a normal UILabel) and now the app work.

    It's very frustrating that I have no information in debug.

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    Can you show the code for the text property?

  • lufo88lufo88 USMember ✭✭✭

    Can you show the code for the text property?

    What you mean?

    It's a simply assignment. The obj has a string property and I assign it to the property text of the UILabel.

  • JohnHJohnH GBMember ✭✭✭✭✭
    Is there any code in the getter of the property. Show the code.
  • lufo88lufo88 USMember ✭✭✭

    I'm not sure I understand what you mean, but this is the code:

    //coming from the underline sqlite db
     public class DtoEntityLight {
     //other fields like categoryId and other infos etc
        public string IdEntity { get; set; }
        public string Name { get; set; }
        public string NameCategory { get; set; }
    
    }
    
    //code in the custom uitableviewcell, this cell was created with the template available in Visual studio Mac
    public void SetData(DtoEntityLight entity) {
                Id = punto.IdEntity;
                NameCat.Text = entity.NameCategory;
                NameEntity.Text = entity.Name;
    }
    

    In the UIViewController who have the reference to UITableView

    [Export("tableView:cellForRowAtIndexPath:")]
    public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
        var cell = tableView.DequeueReusableCell (CellCustom.RESTORATION_ID) as CellCustom;
        if (cell == null) {
            var views = NSBundle.MainBundle.LoadNib ("CellCustom", this, null);
            cell = Runtime.GetNSObject (views.ValueAt (0)) as CellCustom;
        }
    
        var entity = entities[indexPath.Row];
        cell.SetData (entity);
        return cell;
    }
    
  • JohnHJohnH GBMember ✭✭✭✭✭

    Ok so there is no text property there, so the code in the first post isn't the actual code that is having the problem. Which means we can only guess at what the issue might be.
    I would suggest creating a small reproducible example and post that.

  • ColeXColeX Member, Xamarin Team Xamurai

    I don't know if it is related to the freeze but I suggest you not use Runtime class , iOS has some limitation for Dynamic Code Generation , check https://docs.microsoft.com/en-us/xamarin/ios/internals/limitations .

    You could modify your code as below

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
             //register
            tableview.RegisterNibForCellReuse(UINib.FromName("CellCustom", null), CellCustom.RESTORATION_ID);
        }
    
    [Export("tableView:cellForRowAtIndexPath:")]
    public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
    
        var cell = tableView.DequeueReusableCell (CellCustom.RESTORATION_ID) as CellCustom;
    
        var entity = entities[indexPath.Row];
        cell.SetData (entity);
        return cell;
    }
    
  • lufo88lufo88 USMember ✭✭✭

    isn't the actual code that is having the problem.

    Ok, but sure the problem is not in the code I use to get the information from the database, because if I remove that line the scroll work without a problem.

    I will try to modify the xib of the cell, but I wonder why there is no errors or warnings in the log.

  • lufo88lufo88 USMember ✭✭✭
    Accepted Answer

    In the end the problem is a reference to a subclass of UILabel I use in my project. In the xib one of the label isn't of UiLabel but of my custom subclass of UILabel.

    I don't know why this cause a problem (I use the subclass in other View of the app without warnings or errors), but remove this reference (now it is a normal UILabel) and now the app work.

    It's very frustrating that I have no information in debug.

  • lufo88lufo88 USMember ✭✭✭
    edited October 2020

    @ColeX said:
    I don't know if it is related to the freeze but I suggest you not use Runtime class , iOS has some limitation for Dynamic Code Generation , check https://docs.microsoft.com/en-us/xamarin/ios/internals/limitations .

    You could modify your code as below

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
             //register
            tableview.RegisterNibForCellReuse(UINib.FromName("CellCustom", null), CellCustom.RESTORATION_ID);
        }
    
    [Export("tableView:cellForRowAtIndexPath:")]
    public UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) {
    
        var cell = tableView.DequeueReusableCell (CellCustom.RESTORATION_ID) as CellCustom;
    
        var entity = entities[indexPath.Row];
        cell.SetData (entity);
        return cell;
    }
    

    I saw this post now. I wrote that code about 3 years ago and I follow a tutorial in the official xamarin site. I will test your approach (which seems more logical).

Sign In or Register to comment.