Specify the Cell Type

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows you how to change the cell type and therefore the appearance of the table.

Recipe

There are four built-in cell types:

These screenshots show the difference between them. Images are optional. The data and images shown below are included in the sample code.

To specify a different cell type:

  • Update the UITableViewCell constructor in the GetCell method, passing in a different UITableViewCellStyle (uncomment one of the values in this example):
public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
    // request a recycled cell to save memory
    UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
    // if there are no cells to reuse, create a new one
    if (cell == null) {
        cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
        //cell = new UITableViewCell (UITableViewCellStyle.Subtitle, cellIdentifier);
        //cell = new UITableViewCell (UITableViewCellStyle.Value1, cellIdentifier);
        //cell = new UITableViewCell (UITableViewCellStyle.Value2, cellIdentifier);
    }
    cell.TextLabel.Text = tableItems[indexPath.Row];
    // optionally set the other text and image properties here
    return cell;
}
  • If the cell style supports a second line of text, set it like this:
cell.DetailTextLabel.Text = "99 items"; // some text from the data source
  • If the cell style supports an image, set it like this:
cell.ImageView.Image = UIImage.FromFile("Images/somePicture.jpg"); // image filename from the data source

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.