MonoTouch: UITableViewDataSource sections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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