検索機能付きのテーブルビューを実装しようとしています。各アイテムの最初の文字を見る機能があります。次に、一意の文字ごとに、別の配列内に追加します。そのため、 'S' で始まるアイテムが内部にある場合、文字 'S' を 1 回だけ追加します。
次に、この配列を使用して、セクションと rowsForSections を構築します。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rows = 0;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [firstIndex objectAtIndex:section];
//---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@", alphabet];
NSArray *names = [self.filteredListContent filteredArrayUsingPredicate:predicate];
//---return the number of states beginning with the letter---
rows = [names count];
}
else
{
//---get the letter in each section; e.g., A, B, C, etc.---
NSString *alphabet = [firstIndex objectAtIndex:section];
//---get all states beginning with the letter---
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name beginswith[c] %@", alphabet];
NSArray *names = [self.listContent filteredArrayUsingPredicate:predicate];
//---return the number of states beginning with the letter---
rows = [names count];
}
return rows;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
int rows = 0;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
rows = [filteredFirstIndex count];
}
else
{
rows = [firstIndex count];
}
return rows;
}
これが私のCellForRowAtIndexです
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"list content %@",[listContent valueForKey:@"name"]);
static NSString *kCellID = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/*
If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
*/
Contact *contact = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
contact = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
contact = [self.listContent objectAtIndex:indexPath.row];
}
cell.textLabel.text = contact.name;
return cell;
}
問題 ここに私の問題のスクリーンショットがあります。写真は1000以上の言葉を語るからです。
誰でも私を助けてくれることを願っています!
敬具!