Krekin
Krekin

Reputation: 1562

Partially custom UITableViewCell acting funny if there are more than 8 rows

I have a UIViewController that uses a UITableView to display an array. This array gets its data from fetching from Core Data. All works well except having more then 8 entries displayed at once results in rows appearing over other rows when the table view is scrolled. Periodically the text in the rows begins to become slightly fuzzy too. Very weird.

Here is the code of my 'rowForCell" method.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    //Creates a new cell for every row (as in, every time this method is called).
    //UITableViewCell *aCell      = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    //'alloc init' is NOT called here therefore a cell object has to be made inside the table view in the storyboard (which I've done and named cell). This is a more effecient and better way then 'alloc init'. Cells are dequeued & reused.
    UITableViewCell *aCell      = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (aCell == nil)
    {
        aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }


    //Makes sure the separator doesn't have that little gap in the beginning.
    //NOTE: This MUST be here and "_tableView.separatorInset = UIEdgeInsetsZero;" must exist inside [self extraTableViewParameter]!
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        aCell.layoutMargins                     = UIEdgeInsetsZero;
        aCell.preservesSuperviewLayoutMargins   = NO;
    }


    //Cell details.
    aCell.backgroundColor           = [UIColor clearColor];
    aCell.textLabel.textColor       = [UIColor whiteColor];
    //aCell.textLabel.font            = [UIFont fontWithName:@"SanFrancisco" size:20];
    aCell.detailTextLabel.textColor = [UIColor lightGrayColor];
    //aCell.detailTextLabel.font      = [UIFont fontWithName:@"SanFrancisco" size:20];


    //Fill cell with data.
    //NSDictionary *item          = [_tableData objectAtIndex:indexPath.row]; //[_fetchArray objectAtIndex:indexPath.row];
    //AssetsPartners *items       = [_fetchArray objectAtIndex:indexPath.row]; //Using this line of code, instead of the bottom one, will result in _fetchArray NOT being sorted.
    AssetsPartners *items       = [_fetchedResultsController objectAtIndexPath:indexPath]; //Ask '_fetchedResultsController' what's at this particular indexPath right now. Also alphabetically sorts the array list.



    //CUSTOM LABEL instead of standard '.textLabel.text' because 'items.name' is sometimes too long & covers up price: looks messy.
    UILabel *label  = [[UILabel alloc] initWithFrame:CGRectMake(65.0, 15.0, 150.0, 30.0)];
    label.textColor = [UIColor whiteColor];
    label.text      = items.name;
    [aCell.contentView addSubview:label];


    aCell.textLabel.text        = @""; //Make sure this is empty!
    //aCell.textLabel.text        = items.name; //item[@"title"]; //Preiously, was just this with out the UILabel.
    NSInteger anInteger         = [items.balance integerValue];
    aCell.detailTextLabel.text  = [NSString stringWithFormat:@"%@ P", [self addWhitespacesTo:anInteger]]; //item[@"balance"]];


    //Check if it's appropriate to add image to cell or not.
    if (items.photo == nil)
    {
        NSLog(@"No image.");
    }
    else
    {
        NSString *iconName = [self cutOutString:@"bizzer://" fromString:items.photo]; //Get rid of "bizzer://" from string value.


        //Standard UITableViewCell accessors/parameters. Only used to create a clear image to shift text over.
        aCell.imageView.image = [UIImage imageNamed:iconName];  //Inputs appropriate icon into cell image.
        [self turnIcon:aCell.imageView toColor:@"clearColor"];    //Changes icons color accordingly.


        //Entirely new UIImageView for every UITableViewCell (to tweak the frame size & position).
        UIImageView *newImgView = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 15.0, 35.0, 35.0)];
        newImgView.image        = [UIImage imageNamed:iconName];
        [aCell.contentView addSubview:newImgView];
        [self turnIcon:newImgView toColor:items.color];
    }

    return aCell;
}

Upvotes: 1

Views: 63

Answers (2)

Piyush Sharma
Piyush Sharma

Reputation: 1891

Try rearranging your code as follow

if (aCell == nil)
    {
        aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        UILabel *label  = [[UILabel alloc] initWithFrame:CGRectMake(65.0, 15.0, 150.0, 30.0)];
        label.textColor = [UIColor whiteColor];
        label.tag = 999;
        [aCell.contentView addSubview:label];
    }


UILabel *label  = (UILabel *)[aCell.contentView viewWithTag:999];

AssetsPartners *items = [_fetchedResultsController objectAtIndexPath:indexPath]; 
label.text = items.name;

Upvotes: 2

Avi
Avi

Reputation: 7552

You are adding a label without considering that the cell may have been reused. You should move that code into your if (aCell == nil) block so that you only do it once per cell. Same for the image view.

Upvotes: 1

Related Questions