以下是我希望用字母表添加到我的UITableView中的改进:
如果我的表中没有不以字母表中的一个字母开头的结果,我不希望在我的UITableView中看到这个titleForHeaderInSection。
我找不到这样做的方法。
您可以看到我当前的实现和一个示例图像(http://blog.joefusco.name/wp-content/uploads/2010/10/indexed-table.jpg):
facilitiesViewController.h:
@interface FacilitiesViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, ...> {
IBOutlet UITableView *facilitiesTableView;
NSMutableArray *facilitiesDictionary;
NSArray *alphabet;
...
}
@property (nonatomic, retain) NSMutableArray *facilitiesDictionary;
@property (nonatomic, retain) NSArray *alphabet;
...
@endfacilitiesViewController.m:
@implementation FacilitiesViewController
...
- (void)viewDidLoad {
[super viewDidLoad];
alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",
@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
facilitiesDictionary = [[NSMutableArray alloc]init];
for (int i = 0; i < [alphabet count]; i++)
[facilitiesDictionary insertObject:[[NSMutableArray alloc] init] atIndex: i];
... }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alphabet count]; }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[facilitiesDictionary objectAtIndex:section] count]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
return cell; }
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return alphabet; }
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [alphabet indexOfObject:title]; }
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
return [alphabet objectAtIndex:section]; }
@end发布于 2010-11-29 18:09:12
非常感谢Evan,我原以为这样做会很复杂,但事实并非如此!我采用了你的代码,并根据我的代码进行了调整:
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
if ([[facilitiesDictionary objectAtIndex:section] count]==0)
return nil;
return [alphabet objectAtIndex:section];
}发布于 2010-11-27 00:35:13
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
if ([aTableView numberOfRowsInSection:section] == 0) return nil;
return [alphabet objectAtIndex:section];
}UITableView Class Reference
https://stackoverflow.com/questions/4286744
复制相似问题