views:

114

answers:

0

I need 8 rows with a UILabel, UITextField and UISwitch in each row. the Last 2 rows does not contain the UITextField. This is not working for me and not able to determine switch and textfield for a partcular row. When switch is on, textfield value must be saved with it. Please see code below and help!!!:

After visible rows Switch value always points to first switch and gives wrong value:

   - (UITableViewCell *)tableView:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"UITableViewCellStyleSubtitle";

UITableViewCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

      UILabel *fLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 150, 23)] autorelease];
      fLabel.tag = 500;
      fLabel.font = [UIFont boldSystemFontOfSize:15];
      fLabel.textAlignment = UITextAlignmentLeft;
      fLabel.backgroundColor = [UIColor clearColor];
      [cell.contentView addSubview:fLabel];

      highTargetSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(210, 10, 0, 0)];
      highTargetSwitch.tag = indexPath.row;
      [highTargetSwitch addTarget:self action:@selector(switchFliped:) forControlEvents:UIControlEventValueChanged];
      [cell.contentView addSubview:highTargetSwitch];


      highTargetValue = [[UITextField alloc] initWithFrame:CGRectMake(145, 10, 60, 27)];
      highTargetValue.tag = indexPath.row + 12;
      highTargetValue.delegate = self;
      highTargetValue.layer.borderWidth = 1;
      highTargetValue.layer.borderColor = [[UIColor grayColor] CGColor];
      highTargetValue.layer.cornerRadius = 4;
      highTargetValue.textAlignment = UITextAlignmentCenter;
      highTargetValue.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
      [cell.contentView addSubview:highTargetValue];
      NSLog(@"Cell is NIL");

}

 NSUInteger row = indexPath.row;
 UILabel *fLabel  = (UILabel *)[cell.contentView viewWithTag:500];
 fLabel.text = [alertTypes objectAtIndex:row];
 highTargetSwitch = (UISwitch *)[cell.contentView viewWithTag:row];
 highTargetValue  = (UITextField *)[cell.contentView viewWithTag:row + 12];
 NSLog(@"current row is %d", row);
 switch (row) {
      case 0:
           [highTargetSwitch setOn:[bowl.MonitorHighT boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.478];

           break;
      case 1:
           [highTargetSwitch setOn:[bowl.MonitorLowT boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.8];
           break;
      case 2:
           [highTargetSwitch setOn:[bowl.MonitorUp boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.478];
           break;
      case 3:
           [highTargetSwitch setOn:[bowl.MonitorDown boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.478];
           break;
      case 4:
           [highTargetSwitch setOn:[bowl.MonitorPHLow boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.478];
           break;
      case 5:
           [highTargetSwitch setOn:[bowl.MonitorPHHigh boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.478];
           break;
      case 6:
           [highTargetSwitch setOn:[bowl.MonitorVETHigh boolValue]];
           highTargetValue.text = [NSString stringWithFormat:@"%.2f",23.478];
           break;
      case 7:
           [highTargetSwitch setOn:[bowl.MonitorTEGLow boolValue]];
           highTargetValue.hidden = YES; 
           break;
      case 8:
           [highTargetSwitch setOn:[bowl.MonitorTEGHI boolValue]];
           highTargetValue.hidden = YES;
           break;
      default:
           break;
 }

return cell; }

Not sure what's wrong. So this just repeats the first row for all 9 cells even though I set textfield to hidden on last 2. When I put in break points, it does go in the switch statement but no effect.

Also case 0: returns UIView for UISwitch and setON fails.