我在两个部分中实现动态行数时遇到了麻烦。
我的代码如下:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (mySearchBar.text.length > 0)
{
if(section == 0)
{
return @"Exact match(es)";
}
else
{
return @"Additional results";
}
}
else
{
return @"";
NSLog (@"No sections!");
}
}
- (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section
{
if (mySearchBar.text.length > 0)
{
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
NSMutableArray *firstSection = [[NSMutableArray alloc]init];
NSMutableArray *secondSection = [[NSMutableArray alloc]init];
NSUInteger i;
for (i = 0; i <= [self.filteredListContent count] ; i++)
{
// NSLog (@"mysearchbar text %@", mySearchBar.text);
// NSLog (@"self.filtered text %@", [self.filteredListContent objectAtIndex:i]);
//if ([mySearchBar.text isEqualToString: [self.filteredListContent objectAtIndex:i]])
//NSRange s = [mySearchBar.text rangeOfString: [self.filteredListContent objectAtIndex:i]];
//if (s.location!=NSNotFound)
//{
//[mindexes removeAllIndexes];
//[firstSection removeAllObjects];
[firstSection addObjectsFromArray:[self.filteredListContent filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF beginswith[c] %@)", mySearchBar.text]]];
//[firstSection addObject:[self.filteredListContent objectAtIndex:i]];
NSUInteger ind = [firstSection indexOfObject:[self.filteredListContent objectAtIndex:i]];
[mindexes addIndex:ind];
/*
if (section == 0)
{
return [firstSection count];
[tableView reloadData];
}
*/
[self.filteredListContent removeObjectsAtIndexes:mindexes];
//}
//else //if (!(mySearchBar.text == [self.filteredListContent objectAtIndex:i]))
//{
//[secondSection removeAllObjects];
//[secondSection addObject:[self.filteredListContent objectAtIndex:i]];
//secondSection = [NSMutableArray arrayWithArray: firstSection];
//[secondSection removeObjectsAtIndexes: mindexes];
// [self.filteredListContent removeObjectsAtIndexes:mindexes];
/*
if (section == 1)
{
return [secondSection count];
[tableView reloadData];
//return [self.filteredListContent count];
}
*/
//}
}
if (section == 0)
{
return [firstSection count];
//[tableView reloadData];
}
if (section == 1)
{
//return [secondSection count];
return [self.filteredListContent count];
//[tableView reloadData];
//return [self.filteredListContent count];
}
NSLog (@"if return");
}
else // if (mySearchBar.text == nil)
{
//[total release];
return [self.noWords count];
NSLog (@"else return");
}
*/
//Add a return 0; at the end of your method. It's basically a failsafe, if none of the if conditions are met.
return 0;
NSLog (@"return 0!!!");
//If you want to make sure one of the conditions is met, return -1; should cause the application to throw an exception and crash, which might help you track down errors.
//return -1;
}
我想要实现的是,完全匹配(es)仅显示匹配的单词,以及后面的单词(我的数组严格按字母顺序排列),显示在其他结果中,但我得到的是它显示的相同结果在两个部分中,或者在完全匹配中显示多个结果,而在其他结果中仅显示一个结果(与第一部分中的第一个匹配相似)。
对于凌乱的代码,请指点我正确的方向,谢谢。
圣诞快乐!
答案 0 :(得分:1)
因为有2个部分,所以numberOfRowsInSection将运行2次,你应该使用if或者在numberOfRowsInSection的开头切换,用不同的部分编号做不同的工作,这将让你在不同的部分获得正确的ans。