iOS UITableViewStylePlain Разделы и проблема строк - PullRequest
0 голосов
/ 23 декабря 2011

У меня проблемы с реализацией динамического подсчета строк в двух разделах.

Мои коды ниже:

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

}

Что я хочу достичь, так это то, что точное совпадение (я) показывает толькосовпавшие слова и слова, следующие за ним (мои массивы строго в алфавитном порядке), показывают в дополнительных результатах, но все, что я получаю, это то, что он показывает либо одинаковые результаты в обоих разделах, либо несколько результатов в точных совпадениях, тогда как он показывает толькоодин результат (который аналогичен первому совпадению в первом разделе) в дополнительных результатах.

Извините за грязные коды, пожалуйста, укажите мне правильное направление, спасибо.

С Рождеством!

1 Ответ

1 голос
/ 27 декабря 2011

Поскольку существует 2 раздела, поэтому numberOfRowsInSection будет выполняться 2 раза, вам следует использовать if или переключаться в начале numberOfRowsInSection, выполнять другую работу с другим номером раздела, и это позволит вам получить правильный ответ в разных разделах.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...