Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

uitableview - Xamarin.iOS custom cell not showing labels from Interface Builder

I created 2 custom UITableViewCell's in Interface builder and when I dequeue the cells, they are acting as Default cells. I have 2 sections, the first section should be the header labels and they are not connected to any code so I don't need to change their values (set in IB). Second section will contain multiple rows and be connected to data, so I need to set each labels value.

A few things to note:

  • I have double checked that the re-use Identifier is correct in the interface as well as in code
  • And finally, I have registered the the cells like so:

tableView.RegisterClassForCellReuse(typeof(TableInfoDataCell), TableInfoDataCell.cellID);

Some code:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
    try
    {
        switch (indexPath.Section)
        {
            case 1: // Situaltional Data
            { 
                tableView.RegisterClassForCellReuse(typeof(TableInfoDataCell), TableInfoDataCell.cellID);
                TableInfoDataCell cell = (TableInfoDataCell)tableView.DequeueReusableCell(TableInfoDataCell.cellID, indexPath);
                if (cell == null) { cell = new TableInfoDataCell(TableInfoDataCell.cellID); }
                //cell.SetSuccessCriteria = "N";
                cell.BackgroundColor = UIColor.Green;
                cell.TextLabel.Text = "Data Cell";
                return cell;
            }
            default: // Header Cells
            {
                tableView.RegisterClassForCellReuse(typeof(TableInfoHeaderCell), TableInfoHeaderCell.cellID);
                TableInfoHeaderCell cell = (TableInfoHeaderCell)tableView.DequeueReusableCell(TableInfoHeaderCell.cellID, indexPath);
                if (cell == null) { cell = new TableInfoHeaderCell(TableInfoHeaderCell.cellID); }
                cell.BackgroundColor = UIColor.Red;
                cell.TextLabel.Text = "Header Cell";
                return cell;
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("EXCEPTION: " + ex.Message);
        return null;
    }
}

What the table looks like when ran, versus what it should look like (based on Interface Builder design):

enter image description here enter image description here

Custom UITableViewCell class

Both header and data cells are pretty much identical at this point since neither of them will display when loading the tableview.

enter image description here

question from:https://stackoverflow.com/questions/65877898/xamarin-ios-custom-cell-not-showing-labels-from-interface-builder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is my StoryBoard contains with a UITableView and insert a custom TabelViewCell(StudentCell):

enter image description here

First, you need to check the Identifier is setted in StoryBoard:

enter image description here

Then make sure using the same Cell Id in GetCell method:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
    var cell = (StudentCell)tableView.DequeueReusableCell("Cell_ID", indexPath);
    var student = Students[indexPath.Row];
    cell.UpdateCell(student);
    //cell.TextLabel.Text = student.FullName;
    return cell;
}

Last, the custom cell code could modify as follows:

public partial class StudentCell : UITableViewCell
{
    protected StudentCell(IntPtr handle) : base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }

    internal void UpdateCell(Student student)
    {
        //LabelOne/LabelTwo/LabelThree is declared from TabeleViewCell in storyboard 

        LabelOne.Text = student.FullName;
        LabelTwo.Text = student.Course;
        LabelThree.Text = student.Duration;
    }
}

You not need to add AwakeFromNib method.

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...