-2

i newer in Indexing of UITableView. I have some demo data like,

tableDataArray = [[NSMutableArray alloc] init];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:@"Thomas Cook"] forKey:@"Name"];
[dict setObject:[NSString stringWithFormat:@"Dean"] forKey:@"Position"];
[dict setObject:[NSString stringWithFormat:@"thomascook@xyz.com"] forKey:@"Email"];
[dict setObject:[NSString stringWithFormat:@"67-876-65433"] forKey:@"PhoneNo"];
[tableDataArray addObject:dict];

NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:[NSString stringWithFormat:@"John Appleseed"] forKey:@"Name"];
[dict1 setObject:[NSString stringWithFormat:@"Department of accounting"] forKey:@"Position"];
[dict1 setObject:[NSString stringWithFormat:@"john@xyz.com"] forKey:@"Email"];
[dict1 setObject:[NSString stringWithFormat:@"45-876-65433"] forKey:@"PhoneNo"];
 [tableDataArray addObject:dict1];

NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
[dict2 setObject:[NSString stringWithFormat:@"Amar Smith"] forKey:@"Name"];
[dict2 setObject:[NSString stringWithFormat:@"Department of Human Resource"] forKey:@"Position"];
[dict2 setObject:[NSString stringWithFormat:@"amsmith@xyz.com"] forKey:@"Email"];
[dict2 setObject:[NSString stringWithFormat:@"54-876-65433"] forKey:@"PhoneNo"];
[tableDataArray addObject:dict2];

NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init];
[dict3 setObject:[NSString stringWithFormat:@"Mary Cold"] forKey:@"Name"];
[dict3 setObject:[NSString stringWithFormat:@"HOD of Computer Department"] forKey:@"Position"];
[dict3 setObject:[NSString stringWithFormat:@"coldmary@xyz.com"] forKey:@"Email"];
[dict3 setObject:[NSString stringWithFormat:@"52-876-65433"] forKey:@"PhoneNo"];
[tableDataArray addObject:dict3];

NSMutableDictionary *dict4 = [[NSMutableDictionary alloc] init];
[dict4 setObject:[NSString stringWithFormat:@"John Jobs"] forKey:@"Name"];
[dict4 setObject:[NSString stringWithFormat:@"Department of Physics"] forKey:@"Position"];
[dict4 setObject:[NSString stringWithFormat:@"jobs@xyz.com"] forKey:@"Email"];
[dict4 setObject:[NSString stringWithFormat:@"12-876-65433"] forKey:@"PhoneNo"];
[tableDataArray addObject:dict4];

NSMutableDictionary *dict5 = [[NSMutableDictionary alloc] init];
[dict5 setObject:[NSString stringWithFormat:@"Clark Kristan"] forKey:@"Name"];
[dict5 setObject:[NSString stringWithFormat:@"Contact Person"] forKey:@"Position"];
[dict5 setObject:[NSString stringWithFormat:@"kristan@abc.com"] forKey:@"Email"];
[dict5 setObject:[NSString stringWithFormat:@"65-876-65433"] forKey:@"PhoneNo"];
[tableDataArray addObject:dict5];

So , i want to create index UITableView. How will be my UITableView delegate and datasource methods with respective above data format?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
}

Thanks

Maulik
  • 19,348
  • 14
  • 82
  • 137
Tirth
  • 7,801
  • 9
  • 55
  • 88

2 Answers2

0

You need to create an array of titles that you want to show in your index list and return that array in (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { } and in this (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { } just return the index.

for more info read apples doc - http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

saadnib
  • 11,145
  • 2
  • 33
  • 54
  • i want to indexing table data using 1st character of Name, and according to that character should be title of section table header. – Tirth Dec 21 '11 at 04:51
  • yes you need to create an array of all those characters and the second delegate will retrieve that character from the array at the index. – saadnib Dec 21 '11 at 04:53
  • look at this article it will help you - http://www.iphonesdkarticles.com/2009/01/uitableview-indexed-table-view.html – saadnib Dec 21 '11 at 04:54
0

There is a very sweet article about the UITableView indexing.

here is the link for it.

http://www.iphonedevcentral.com/indexed-uitableview-tutorial/

Ajeet Pratap Maurya
  • 4,244
  • 3
  • 28
  • 46