Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

straber

macrumors member
Original poster
Jul 3, 2012
60
0
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:

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:
 

Attachments

  • alarm_table.jpg
    alarm_table.jpg
    11.7 KB · Views: 276
Also, are you sure you want to be instantiating a new UISwitch every time cellForRowAtIndexPath: is called?

Well if you look at the selector that the switch calls upon ValueDidChange it is updateSwitchAtIndexPath, so correct me if I'm wrong, if he just used a singular UISwitch, wouldn't that method possibly update all of the visible UISwitches (probably not what he/she wants).
 
Well if you look at the selector that the switch calls upon ValueDidChange it is updateSwitchAtIndexPath, so correct me if I'm wrong, if he just used a singular UISwitch, wouldn't that method possibly update all of the visible UISwitches (probably not what he/she wants).

Yeah, I'm not suggesting a singular UISwitch. I'm suggesting that the UISwitch be instantiated when the rest of the cell is (i.e. not dequeued), in order to prevent excessive instantiation.
 
Last edited:
Yeah, I'm not suggesting a singular UISwitch. I'm suggesting that the UISwitch be instantiated when the rest of the cell is (i.e. not dequeued), in order to prevent excessive instantiation.

I gotcha, just for my own reference, where would he place the UISwitch instantiation code to achieve this?
 
Inside the if (nil == cell) block. Of course, then there's the task of getting a reference to the switch when the cell was dequeued and not instantiated.

Again, for my own reference, would it work to create a custom Cell (which it looks like OP did) and create a property on the cell which is a UI switch?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.