UISwitch и UIButton в UITableViewCell, как их идентифицировать

У меня небольшая проблема с моей UITableViewCell. Каждая пользовательская ячейка имеет UISwitch и UIButton. И когда пользователь меняет значение UISwitch, я хотел бы установить для свойства button.hidden значение YES.

Я могу найти, какой UISwitch был нажат с помощью этого метода:

- (IBAction)closeHour:(id)sender {

     UISwitch  *senderSwitch = (UISwitch *)sender;
     UITableViewCell *switchCell = (UITableViewCell *)[senderSwitch superview];
     NSUInteger buttonRow = [[resas indexPathForCell:switchCell] row];
     NSLog("%d", buttonRow);
}

Это работает отлично, но я не знаю, как получить UIButton с тем же индексом (indexPath.row), чтобы установить его скрытое свойство. Я думал о том, чтобы установить тег в каждом UIButton, но я не очень дружелюбен с ними.

Вот как была создана моя ячейка. Если кто-нибудь скажет мне, делаю ли я здесь какую-то ерунду, это будет очень хорошо:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  { 
     static NSString   *cellIdentifier1 = @"Cell1";
     static NSString   *cellIdentifier2 = @"Cell2";

     if ([typeOfData objectAtIndex:indexPath.row] == @"hour") {
         TimeCell   *cell = (TimeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];

      if (cell == nil) {
          cell = [[[TimeCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier1] autorelease];
          UISwitch *isOpen = [[UISwitch alloc] initWithFrame:CGRectMake(300, 7, 0, 0)];

          if ([self openOrCloseHour:[[[finalData objectAtIndex:indexPath.row] substringToIndex:2] intValue]])
              isOpen.on = YES;
          else {
              isOpen.on = NO;
              [isOpen addTarget:self action:@selector(closeHour:) forControlEvents:UIControlEventValueChanged];
              [cell addSubview:isOpen];
              [isOpen release];
              UIButton   *add = [UIButton buttonWithType:UIButtonTypeContactAdd];
              add.frame = CGRectMake(400, 3, 170, 40);
              [add addTarget:self action:@selector(addResa:) forControlEvents:UIControlEventTouchDown];
              [cell addSubview:add];
          }
      }
      [cell.time setText:[finalData objectAtIndex:indexPath.row]];
      return cell;
   }
   else 
   {
         ResaCell   *cell = (ResaCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2];

         if (cell == nil) {
             cell = [[[ResaCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier2] autorelease];
             [cell.isConfirm setImage:[UIImage imageNamed:@"confirm.png"]];
             [cell.nom  setText:[finalData objectAtIndex:indexPath.row]];
             [cell.prenom  setText:[finalData objectAtIndex:indexPath.row]];
             [cell.arrive  setText:[finalData objectAtIndex:indexPath.row]];
         }

         return cell;
    }

    return nil;
}

Для информации, у меня есть два типа ячеек. Проблема на TimeCell.

Есть ли у кого-нибудь решение?

1
задан MathieuF 27 May 2015 в 11:21
поделиться