There seems to be a ton of questions on this but I am not able to use anyone else's answers so was hoping someone could review how I am doing this. I am trying to use. I have two custom UITableViewCells which right now just have a BOOL property on them and thats it in the way of styling.
Within my cellForRowAtIndexPath method based on what type of data is coming back I am styling my cells. If the data is a "month" header its a long skinny looking cell and if its a "news item" its going to be a larger white looking cell.
When the table loads everything looks great but if I scroll down to create more cells and then scroll back up the cells are being recreated and eventually scrolling slows down because I am running out of memory.
When I set break points the dequeueReusableCellWithIdentifier always returns nil so my cells are never reused which seems to be a problem.
In this picture you can see that cells are getting stacked on top of each other and messed up:

Here my my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NewsCellIdentifer = @"NewsCellIdentifier";
static NSString *MonthCellIdentifier = @"MonthCellIdentifier";
NSUInteger row = [indexPath row];
NewsItem *item = [self.newsArray objectAtIndex:row];
if (item.IsMonth == YES)
{
NewsMonthUITableViewCell *cell = [self.mytableView dequeueReusableCellWithIdentifier:MonthCellIdentifier];
if (cell == nil)
{
cell = [[NewsMonthUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MonthCellIdentifier];
}
// This handles any other "date" cells to allow for different spacing styles.
if (item.IsMonth)
{
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 400, 20)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"month-bar-bkgd.png"];
UILabel *monthTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 20)];;
CGFloat font = 11.0f;
monthTextLabel.font = [BVFont HelveticaNeue:&font];
monthTextLabel.backgroundColor = [UIColor clearColor];
monthTextLabel.font = [BVFont HelveticaNeue:&font];
monthTextLabel.textColor = [BVFont WebGrey];
monthTextLabel.text = item.Title;
cell.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:av];
[cell.contentView addSubview:monthTextLabel];
}
return cell;
}
else
{
NewsUITableViewCell *cell = [self.mytableView dequeueReusableCellWithIdentifier:NewsCellIdentifer];
if (cell == nil)
{
cell = [[NewsUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewsCellIdentifer];
}
cell.contentView.backgroundColor = [UIColor clearColor];
UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,100)];
whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
whiteRoundedCornerView.layer.masksToBounds = NO;
whiteRoundedCornerView.layer.cornerRadius = 3.0;
whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
whiteRoundedCornerView.layer.shadowOpacity = 0.5;
[cell.contentView addSubview:whiteRoundedCornerView];
[cell.contentView sendSubviewToBack:whiteRoundedCornerView];
[cell.contentView addSubview:[self NewsItemThumbnailView:item]];
[cell.contentView addSubview:[self NewsItemTextView:item]];
[cell.contentView addSubview:[self NewsItemCornerIconIndicatorView:item]];
return cell;
}
return nil;
}
Thanks for any assistance or advice!
As you are using Storyboards WITHOUT dynamic prototypes then you need to place your code that creates and adds sub-views INSIDE your if (cell==nil) block. Otherwise all the sub-views are added again each time the tableview cell is re-used.
Going forwards, my advice is to use Storyboards with dynamic prototypes (with sub-classed UITableViewCells) and do the customisation in the cells in IB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With