Reputation: 50707
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:
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):
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.
Upvotes: 0
Views: 147
Reputation: 12723
Here is my StoryBoard contains with a UITableView and insert a custom TabelViewCell(StudentCell):
First, you need to check the Identifier
is setted in StoryBoard:
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.
Upvotes: 1