Heena
Heena

Reputation: 754

iphone: problem reloading table data

I am using custom label in table cell.everytime i visit again this page the label text getting more darker like it is behaving overwriting. how can i fix this?

  - (void)viewWillAppear:(BOOL)animated {

    NSFetchRequest *request = [[NSFetchRequest alloc] init];

        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Note" inManagedObjectContext:context];
        [request setEntity:entity]; 
        [request release];    
        [self.tableView reloadData];
    }

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

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }

        // Set up the cell...
        Note *noteItem = [resultController objectAtIndexPath:indexPath];

        //[cell.textLabel setText:[noteItem noteTitle]];    
        //[cell.detailTextLabel setText:[dateFormatter stringFromDate:[noteItem creationDate]]];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];

        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 19)];
        newLabel.text = [noteItem noteTitle];
        [newLabel setBackgroundColor:[UIColor clearColor]];
        [cell addSubview:newLabel];
        [newLabel release];

        UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, 200, 26)];
        detailLabel.text = [dateFormatter stringFromDate:[noteItem creationDate]];
        [detailLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
        [detailLabel setBackgroundColor:[UIColor clearColor]];
        [cell addSubview:detailLabel];
        [detailLabel release];

        [cell setBackgroundColor:[UIColor clearColor]];
         [cell setAlpha:0.6];
        return cell;
    }

Upvotes: 0

Views: 192

Answers (3)

Ishu
Ishu

Reputation: 12787

your cellForRowAtIndexPath method should look like-

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

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

           nameLabelFrame = CGRectMake(5, 5, 200, 19);
        countLabelFrame = CGRectMake(5, 20, 200, 26);
          UILabel *lblTemp;
            lblTemp = [[UILabel alloc] initWithFrame:nameLabelFrame];
        lblTemp.tag = 1;
        [cell.contentView addSubview:lblTemp];
        [lblTemp release];


        lblTemp = [[UILabel alloc] initWithFrame:countLabelFrame];
        lblTemp.tag = 2;
        [cell.contentView addSubview:lblTemp];
        [lblTemp release];

        }

        // Set up the cell...
        Note *noteItem = [resultController objectAtIndexPath:indexPath];

UILabel *newLabel = (UILabel *)[cell viewWithTag:1];
        newLabel.text = [noteItem noteTitle];
        [newLabel setBackgroundColor:[UIColor clearColor]];



        UILabel *detailLabel = (UILabel *)[cell viewWithTag:2];
        detailLabel.text = [dateFormatter stringFromDate:[noteItem creationDate]];
        [detailLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
        [detailLabel setBackgroundColor:[UIColor clearColor]];



        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        cell.accessoryView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];



        [cell setBackgroundColor:[UIColor clearColor]];
         [cell setAlpha:0.6];
        return cell;
    }

Upvotes: 0

iProgrammer
iProgrammer

Reputation: 3107

Create UILabel in

if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }

and Assign Its Value outside of this condition

Upvotes: 2

Man of One Way
Man of One Way

Reputation: 3980

You should check if the cell == nil. If so, then add all the labels again, otherwise, they have already been added.

Upvotes: 0

Related Questions