UITwitch和UITutton在UITableViewCell中,如何识别它们

时间:2010-10-08 08:27:46

标签: ios iphone uitableview uibutton uiswitch

我的UITableViewCell有点问题。 每个Custom Cell都有一个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);
}

这项工作完美,但我不知道如何在相同的索引(indexPath.row)中设置UIButton来设置其隐藏属性。我想在每个UIButton中设置一个标签,但我对这些标签不是很友好。

我的Cell是如何创建的,如果有人可以告诉我,如果我在做一些废话,那可能会非常好看:

- (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 个答案:

答案 0 :(得分:2)

在cellForRow中:

isOpen.tag = 2000+indexPath.row;
add.tag = 4000+indexPath.row;

要在closeBour IBACtion中获取UIButton:

int tagButton = senderSwitch.tag-2000+4000;
button = (UIButton*)[self.view viewWithTag:tagButton];