【问题标题】:How i Indexing of UITableView?我如何索引 UITableView?
【发布时间】:2012-01-24 23:45:54
【问题描述】:

我更新了 UITableView 的索引。 我有一些演示数据,例如,

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];

所以,我想创建索引 UITableView。我的 UITableView 委托和数据源方法将如何分别具有上述数据格式?

- (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 {
}

谢谢

【问题讨论】:

  • 请详细说明您的问题。您希望如何显示数据?
  • @Maulik,我想使用名称的第一个字符来索引 tableview 数据。

标签: iphone ipad ipod-touch


【解决方案1】:

有一篇关于 UITableView 索引的非常甜蜜的文章。

这是它的链接。

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

【讨论】:

    【解决方案2】:

    您需要创建要在索引列表中显示的标题数组,并在(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { } 和此(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { } just return the index. 中返回该数组

    欲了解更多信息,请阅读苹果文档 - http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html

    【讨论】:

    • 我想使用名称的第一个字符来索引表数据,根据该字符应该是节表头的标题。
    • 是的,您需要创建一个包含所有这些字符的数组,第二个委托将从索引处的数组中检索该字符。
    • 看看这篇文章对你有帮助 - iphonesdkarticles.com/2009/01/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多