Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created February 15, 2013 22:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save yetanotherchris/4963935 to your computer and use it in GitHub Desktop.
MonoTouch: UITableViewDataSource sections
public class MyDataSource : UITableViewDataSource
{
IList<SectionData> _data;
private class SectionData
{
public string Title { get; set; }
public string CellId { get; set; }
public IList<string> Data { get; set; }
public SectionData(string cellId)
{
Title = "";
CellId = cellId;
Data = new List<string>();
}
}
public MyDataSource()
{
_data = new List<SectionData>();
SectionData section1 = new SectionData("section1");
section1.Data.Add("item 1");
section1.Data.Add("item 2");
section1.Data.Add("item 3");
section1.Title = "Basic";
_data.Add(section1);
SectionData section2 = new SectionData("section2");
section2.Data.Add("option a");
section2.Data.Add("option b");
section2.Title = "Advanced";
_data.Add(section2);
}
public override string TitleForHeader(UITableView tableView, int section)
{
return _data[section].Title;
}
public override int RowsInSection(UITableView tableview, int section)
{
return _data[section].Data.Count;
}
public override int NumberOfSections(UITableView tableView)
{
return _data.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
SectionData sectionData = _data[indexPath.Section];
string cellId = sectionData.CellId;
string row = sectionData.Data[indexPath.Row];
UITableViewCell cell = tableView.DequeueReusableCell(cellId);
if (cell == null)
cell = new UITableViewCell(UITableViewCellStyle.Default, cellId);
cell.TextLabel.Text = row;
return cell;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment