I'm trying to create a custom UITableViewCell, but part of it renders with a gray background and part of it with a white background. The gray stops right at the point where the UISwitch begins, but I can't figure out why.
Can anyone figure out why this happens?
Here's the code for the table cell:
Here's what happens:
Can anyone figure out why this happens?
Here's the code for the table cell:
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
AlarmTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AlarmTableCell" owner:self options:nil];
for (id oneObject in nib) {
if ([oneObject isKindOfClass:[AlarmTableCell class]]) {
cell = (AlarmTableCell *)oneObject;
}
}
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editingAccessoryType = UITableViewCellAccessoryNone;
UISwitch *switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
switchview.tag = [indexPath row];
[switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = switchview;
NSUInteger row = [indexPath row];
Alarm *a = [alarms objectAtIndex:row];
NSArray *comps = [a.time componentsSeparatedByString:@":"];
NSString *ampm;
if ([(NSString *)[comps objectAtIndex:0] intValue] < 12) {
ampm = @"AM";
}
else {
ampm = @"PM";
}
int h = [(NSString *)[comps objectAtIndex:0] intValue];
if (h == 0) {
h = 12;
}
else if (h > 12) {
h -= 12;
}
NSMutableString *repeatDays = [[NSMutableString alloc] init];
repeatDays = [self matchPattern:a];
if ([repeatDays isEqualToString:@""]) {
for (NSNumber *num in a.repeatDays) {
switch ([num intValue]) {
case 1:
[repeatDays appendString:@"Sun "];
break;
case 2:
[repeatDays appendString:@"Mon "];
break;
case 3:
[repeatDays appendString:@"Tue "];
break;
case 4:
[repeatDays appendString:@"Wed "];
break;
case 5:
[repeatDays appendString:@"Thur "];
break;
case 6:
[repeatDays appendString:@"Fri "];
break;
case 7:
[repeatDays appendString:@"Sat "];
break;
default:
break;
}
}
}
cell.timeLabel.text = [NSString stringWithFormat:@"%i:%@", h, (NSString *)[comps objectAtIndex:1]];
cell.ampmLabel.text = ampm;
cell.repeatLabel.text = repeatDays;
switchview.on = a.onoff;
cell.contentView.backgroundColor = [UIColor lightGrayColor];
cell.contentView.layer.borderColor = [UIColor redColor].CGColor;
return cell;
}
Here's what happens: