chrisladd / CGLAlphabetizer

A simple class to easily alphabetize an array of objects.
MIT License
24 stars 3 forks source link

Contents of the cell superimposed in iOS7 #1

Closed KarianSilva closed 10 years ago

KarianSilva commented 10 years ago

Hi! I'm trying to use your component, but i had compatibility issues with iOS7. In iOS6 everything is working perfectly. But when I use iOS7 the content of cells is superimposed on each scroll. In my view there must be something wrong with the 'objectAtIndexPath' method because overlapping content is an equivalent line of my source ArrayList.

Below the pictures iOS6 (correct) and iOS7 (bug)

captura de tela 2014-05-09 as 17 58 32

captura de tela 2014-05-09 as 17 46 25

chrisladd commented 10 years ago

Hi @KarianSilva , is there any chance you can send me a little example project to try to reproduce the problem? Or maybe you can paste the code from your UITableView datasource on this ticket?

KarianSilva commented 10 years ago
- (void)setSectionsAlphabetizer
{

    self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:medicamentoLista usingKeyPath:@"medicamento"];
    self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.resultado_TableView reloadData];
}

- (Medicamento *)objectAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.sectionIndexTitles;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.sectionIndexTitles count];
}

- (NSString *)controller:(NSFetchedResultsController *)controller sectionIndexTitleForSectionName:(NSString *)sectionName
{
    return sectionName;}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.sectionIndexTitles[section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    NSString *sectionIndexTitle = self.sectionIndexTitles[section];
    return [self.alphabetizedDictionary[sectionIndexTitle] count];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *headerView = [[UIView alloc] init];

    headerView.frame = CGRectMake(0, 0, 308, 30);

    headerView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"headreSection"]];

    UILabel *testLabel = [[UILabel alloc] init];
    testLabel.frame = CGRectMake(10, 2, 320, 20);
    testLabel.font = [UIFont fontWithName:@"Futura" size:15];
    testLabel.textColor = [UIColor grayColor];
    testLabel.text = [self tableView:tableView titleForHeaderInSection:section];
    testLabel.backgroundColor = [UIColor clearColor];

    [headerView addSubview:testLabel];

    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CGLContactsCellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CGLContactsCellIdentifier ];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    Medicamento *medicamentoCelula = [self objectAtIndexPath:indexPath];

    UILabel *labelMedicamento = [[UILabel alloc]initWithFrame:CGRectMake(32, 10, 268, 21)];

    UILabel *labelSubstancia = [[UILabel alloc]initWithFrame:CGRectMake(32, 36, 255, 12)];

    UIImageView *imgFavoritoImageView;

    if (medicamentoCelula.favorito != NULL) {

        imgFavoritoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Fav_Buscar_On"]];
    }

    else
    {
        imgFavoritoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Fav_Buscar_Off"]];
    }

    CGFloat nRed=226.0/255.0;
    CGFloat nBlue=235.0/255.0;
    CGFloat nGreen=230.0/255.0;
    UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1];

    resultado_TableView.separatorColor = myColor;

    nRed=46.0/255.0;
    nBlue=71.0/255.0;
    nGreen=143.0/255.0;

    myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1];

    labelMedicamento.textColor = myColor;
    labelSubstancia.textColor = [UIColor grayColor];

    labelMedicamento.font = [UIFont fontWithName:@"Futura" size:17];
    labelSubstancia.font = [UIFont fontWithName:@"Futura" size:10];

    labelMedicamento.text = medicamentoCelula.medicamento;
    labelSubstancia.text = medicamentoCelula.substancia;

    [cell.contentView addSubview:labelMedicamento];
    [cell.contentView addSubview:labelSubstancia];

    NSString *laboratorioNome = [self carregaLaboratorio:[medicamentoCelula.l_id intValue]];

    if ((labelMedicamento.text.length <= 20 && laboratorioNome.length <= 22) ||
        (labelMedicamento.text.length <= 22 && laboratorioNome.length <= 20) ||
        (labelMedicamento.text.length <= 23 && laboratorioNome.length <= 18) ||
        (labelMedicamento.text.length <= 26 && laboratorioNome.length <= 15))
    {

        UILabel *labelLaboratorio;

        if (labelMedicamento.text.length >= 26)
        {
            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(235, 17, 50, 10)];

        }

        else if (labelMedicamento.text.length >= 21)
        {
            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(225, 17, 60, 10)];
        }

        else if (labelMedicamento.text.length >=18)
        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(195, 17, 90, 10)];

        }

        else if (labelMedicamento.text.length >=15)
        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(185, 17, 100, 10)];

        }

        else
        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(175, 17, 110, 10)];

        }

        nRed=106/255.0;
        nBlue=122/255.0;
        nGreen=178/255.0;

        myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1];

        labelLaboratorio.textColor = myColor;

        labelLaboratorio.font =[UIFont fontWithName:@"Futura" size:9];

        labelLaboratorio.textAlignment = NSTextAlignmentRight;

        [cell.contentView addSubview:labelLaboratorio];

        labelLaboratorio.text = laboratorioNome;

        [cell.contentView addSubview:imgFavoritoImageView];

    }

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [buscarMedicamento_SearchBar resignFirstResponder];

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    ListaBulasViewController *listaBulasView = [storyBoard instantiateViewControllerWithIdentifier:@"ListaBulasViewController"];

    Medicamento *medicamentoSelecionado = [self objectAtIndexPath:indexPath];

    listaBulasView.medicamentoSelecionadoViewAnterior = medicamentoSelecionado;

    [listaBulasView setTitle:medicamentoSelecionado.medicamento];

    [self.navigationController pushViewController:listaBulasView animated:YES];

}
KarianSilva commented 10 years ago

- (void)setSectionsAlphabetizer

{

    self.alphabetizedDictionary = [CGLAlphabetizer
alphabetizedDictionaryFromObjects:medicamentoLista usingKeyPath:
@"medicamento"];

    self.sectionIndexTitles = [CGLAlphabetizer
indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];

    [self.resultado_TableView reloadData];

}

- (Medicamento *)objectAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section
];

    return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];

}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    return self.sectionIndexTitles;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return [self.sectionIndexTitles count];

}

- (NSString *)controller:(NSFetchedResultsController *)controller
sectionIndexTitleForSectionName:(NSString *)sectionName

{

    return sectionName;}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(
NSInteger)section

{

    return self.sectionIndexTitles[section];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(
NSInteger)section

{

    NSString *sectionIndexTitle = self.sectionIndexTitles[section];

    return [self.alphabetizedDictionary[sectionIndexTitle] count];

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(
NSInteger)section

{

    UIView *headerView = [[UIView alloc] init];

    headerView.frame = CGRectMake(0, 0, 308, 30);

    headerView.backgroundColor = [[UIColor alloc] initWithPatternImage:[
UIImage imageNamed:@"headreSection"]];

    UILabel *testLabel = [[UILabel alloc] init];

    testLabel.frame = CGRectMake(10, 2, 320, 20);

    testLabel.font = [UIFont fontWithName:@"Futura" size:15];

    testLabel.textColor = [UIColor grayColor];

    testLabel.text = [self tableView:tableView titleForHeaderInSection
:section];

    testLabel.backgroundColor = [UIColor clearColor];

    [headerView addSubview:testLabel];

    return headerView;

}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CGLContactsCellIdentifier forIndexPath:indexPath];

    if (cell == nil) {

        cell = [[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleDefault reuseIdentifier:CGLContactsCellIdentifier ];

    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    Medicamento *medicamentoCelula = [self objectAtIndexPath:indexPath];

    UILabel *labelMedicamento = [[UILabel alloc]initWithFrame:CGRectMake(32,
10, 268, 21)];

    UILabel *labelSubstancia = [[UILabel alloc]initWithFrame:CGRectMake(32,
36, 255, 12)];

    UIImageView *imgFavoritoImageView;

    if (medicamentoCelula.favorito != NULL) {

        imgFavoritoImageView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"Fav_Buscar_On"]];

    }

    else

    {

        imgFavoritoImageView = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:@"Fav_Buscar_Off"]];

    }

    CGFloat nRed=226.0/255.0;

    CGFloat nBlue=235.0/255.0;

    CGFloat nGreen=230.0/255.0;

    UIColor *myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen
alpha:1];

    resultado_TableView.separatorColor = myColor;

    nRed=46.0/255.0;

    nBlue=71.0/255.0;

    nGreen=143.0/255.0;

    myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen alpha:1
];

    labelMedicamento.textColor = myColor;

    labelSubstancia.textColor = [UIColor grayColor];

    labelMedicamento.font = [UIFont fontWithName:@"Futura" size:17];

    labelSubstancia.font = [UIFont fontWithName:@"Futura" size:10];

    labelMedicamento.text = medicamentoCelula.medicamento;

    labelSubstancia.text = medicamentoCelula.substancia;

    [cell.contentView addSubview:labelMedicamento];

    [cell.contentView addSubview:labelSubstancia];

    NSString *laboratorioNome = [self carregaLaboratorio:[medicamentoCelula.
l_id intValue]];

    if ((labelMedicamento.text.length <= 20 && laboratorioNome.length <= 22)
||

        (labelMedicamento.text.length <= 22 && laboratorioNome.length <= 20)
||

        (labelMedicamento.text.length <= 23 && laboratorioNome.length <= 18)
||

        (labelMedicamento.text.length <= 26 && laboratorioNome.length <= 15
))

    {

        UILabel *labelLaboratorio;

        if (labelMedicamento.text.length >= 26)

        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(235,
17, 50, 10)];

        }

        else if (labelMedicamento.text.length >= 21)

        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(225,
17, 60, 10)];

        }

        else if (labelMedicamento.text.length >=18)

        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(195,
17, 90, 10)];

        }

        else if (labelMedicamento.text.length >=15)

        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(185,
17, 100, 10)];

        }

        else

        {

            labelLaboratorio = [[UILabel alloc]initWithFrame:CGRectMake(175,
17, 110, 10)];

        }

        nRed=106/255.0;

        nBlue=122/255.0;

        nGreen=178/255.0;

        myColor=[[UIColor alloc]initWithRed:nRed green:nBlue blue:nGreen
alpha:1];

        labelLaboratorio.textColor = myColor;

        labelLaboratorio.font =[UIFont fontWithName:@"Futura" size:9];

        labelLaboratorio.textAlignment = NSTextAlignmentRight;

        [cell.contentView addSubview:labelLaboratorio];

        labelLaboratorio.text = laboratorioNome;

        [cell.contentView addSubview:imgFavoritoImageView];

    }

    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(
NSIndexPath *)indexPath

{

    [buscarMedicamento_SearchBar resignFirstResponder];

    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:
@"MainStoryboard" bundle:nil];

    ListaBulasViewController *listaBulasView = [storyBoard
instantiateViewControllerWithIdentifier:@"ListaBulasViewController"];

    Medicamento *medicamentoSelecionado = [self objectAtIndexPath
:indexPath];

    listaBulasView.medicamentoSelecionadoViewAnterior =
medicamentoSelecionado;

    [listaBulasView setTitle:medicamentoSelecionado.medicamento];

    [self.navigationController pushViewController:listaBulasView animated:
YES];

}

2014-05-09 19:35 GMT-03:00 Chris Ladd <notifications@github.com>:

> Hi @KarianSilva <https://github.com/KarianSilva> , is there any chance
> you can send me a little example project to try to reproduce the problem?
> Or maybe you can paste the code from your UITableView datasource on this
> ticket?
>
> —
> Reply to this email directly or view it on GitHub<https://github.com/chrisladd/CGLAlphabetizer/issues/1#issuecomment-42720756>
> .
>

-- 

Att

*Karian Silva*

*Quality Analyst / Test Analyst *

*ISTQB - CTFL –  Certified Tester*

*MCP - MTA – Software Development*

*MCP - MTA – Database Administration*
chrisladd commented 10 years ago

Hi @KarianSilva - here's your problem:


    UILabel *labelMedicamento = [[UILabel alloc]initWithFrame:CGRectMake(32, 10, 268, 21)];

    UILabel *labelSubstancia = [[UILabel alloc]initWithFrame:CGRectMake(32, 36, 255, 12)];

. . . 

    [cell.contentView addSubview:labelLaboratorio];

Because you're creating these labels and adding them to your tableview cell in your datasource, you're adding more and more labels to your cells each time a cell is reused.

You should instead create your own subclass of UITableViewCell, with each of your custom labels as @properties.

Apple has a great guide for this.

Thanks for using CGLAlphabetizer, and let me know if you run into any other issues!

KarianSilva commented 10 years ago

Really?

Good, as it worked perfectly in the iOS6, I had thinking that the code was correct.

I'll make those changes you said and come back here to tell if it worked!!

Many thanks for providing and sharing this with us.

=)