UITableViewCell中的UISwitch和UIButton,如何识别它们

发布于 2024-09-26 07:07:54 字数 2663 浏览 10 评论 0原文

我的 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);
}

这完美地工作,但我不知道如何在同一索引(indexPath.row)处获取 UIButton 来设置他的隐藏属性。我考虑过在每个 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 上。

有人有解决办法吗?

I've got a little problem in my UITableViewCell.
Each Custom Cell have an UISwitch and an UIButton. And when the user change the value of the UISwitch i would like to set the button.hidden property to YES.

I can find which UISwitch was clicked with this method:

- (IBAction)closeHour:(id)sender {

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

This work perfectly, but i don't know how to get the UIButton at the same index (indexPath.row) to set his hidden property. I thought about setting a tag in each UIButton but i'm not very friendly with those.

There is how my Cell was created, if someone can tell me if i am doing some crap here it could be very nice:

- (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;
}

For information I have got two type of cell. The problem is on TimeCell.

Is anybody have the solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-10-03 07:07:54

在 cellForRow 中:

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

要在 closeHour IBACtion make 中获取 UIButton:

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

In the cellForRow :

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

To get the UIButton in the closeHour IBACtion make :

int tagButton = senderSwitch.tag-2000+4000;
button = (UIButton*)[self.view viewWithTag:tagButton];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文