Thursday, 26 September 2013

UILabels within a UITableView cell are overlaying each other in iOS7

UILabels within a UITableView cell are overlaying each other in iOS7

I didn't have this problem with iOS6, but am currently having it with
iOS7. I have a UITableView and you can see 8 cells at the time the view is
loaded. Each populated with different names from and array. If you scroll
down, the next two cells look good, but everything past that gets text
laid on top of it; That text being the contents of what was in the
previous cells. So the 10th cell will have what was in the first cell, as
well as what is supposed to be in the 10th cell laid on top of it.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//Create Label for Name inside cell
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake( 7.0,
5.0, 300.0, 30.0 )];
[name setText:[self.entriesArray objectAtIndex:indexPath.row]];
//Check to see if current person is a Parent or Child
NSString *class = [self.database
getCellDataWithMembership:[self.MembershipArray
objectAtIndex:indexPath.row] andColIndex:4];
if([class isEqualToString:@"CHILD"])
{
name.textColor = [UIColor colorWithRed:25.0f/255.0f
green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
name.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
}
[cell.contentView addSubview:name];
return cell;
}
My skill with Table views is makeshift at best. I've been reading lots of
documentation and researching solutions, but was not able to come up with
a solution. I just find it odd that it works perfect for iOS6, but not for
iOS7.
So it fetches a person's name from an array and I want to populate the
cells with those names. I was able to originally accomplish this using:
cell.textLabel.text = [self.entriesArray objectAtIndex:indexPath.row];
if([class isEqualToString:@"CHILD"])
{
cell.textLabel.textColor = [UIColor colorWithRed:25.0f/255.0f
green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];
If I use that instead of the "name" UILabel actions from the first code
block, then it displays the names perfectly with no text overlay, but what
becomes an issue is the text color. If they are labeled as a CHILD then
they should be green text and bold. However, after scrolling down, every
person becomes green when they shouldn't be.
Sorry for the lengthy question. I've been working on this and racking my
brain around it and I just can't seem to figure it out. Any help would be
greatly appreciated.

No comments:

Post a Comment