can't configure table cell right

Discussion in 'iOS Programming' started by straber, Feb 10, 2013.

  1. straber macrumors member

    Joined:
    Jul 3, 2012
    #1
    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:
     

    Attached Files:

  2. seepel macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #2
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Also, are you sure you want to be instantiating a new UISwitch every time cellForRowAtIndexPath: is called?
     
  4. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #4
    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).
     
  5. dejo, Feb 11, 2013
    Last edited: Feb 11, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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.
     
  6. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #6
    I gotcha, just for my own reference, where would he place the UISwitch instantiation code to achieve this?
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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.
     
  8. waterskier2007 macrumors 68000

    waterskier2007

    Joined:
    Jun 19, 2007
    Location:
    White Lake, MI
    #8
    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?
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Yes, that'll work too.
     
  10. Chirone macrumors 6502

    Joined:
    Mar 2, 2009
    Location:
    NZ
    #10
    Yes. That would be a more correct thing to do.
     

Share This Page