0

我在故事板中定义了三个原型单元UISegmentedControl,每个单元都包含一个小部件。我有两种标准可以向用户展示;一个是[ON|OFF]另一个是[1|2|3|4]。我需要以编程方式设置每个小部件的文本,因此我Tag在情节提要中设置小部件,以便使用[cell viewWithTag:XX]. 问题是,我所有的UISegmentedControl小部件都连接到同一个侦听器,我无法判断用户正在打开或关闭哪些标准。我已经看到Tag设置为行号的值,cellForRowAtIndexPath但我已经Tag在情节提要中使用了。我怎样才能知道在我的侦听器中正在与哪一行进行交互?

更新:添加了代码片段

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   UITableViewCell *retcell;
   NSInteger thisrow = indexPath.row;

   // retrieve the items which should be printed for this
   // row. set text on widget depending on item type
   OBJ_Items *items = [mCriteria objectForKey:thisrow];

   if ([items.Type isEqualToString:YESNO])
   {
      retcell = [tableView dequeueReusableCellWithIdentifier:@"yesno"];

      // get a handle to the widget in this cell in order to access it's Text property
      UISegmentedControl *yesno = (UISegmentedControl *)[retcell viewWithTag:200];

      // .. set text for yesno segments

      // save the row numer to this object's tag so we can retrieve the item object
      // on click
      [yesno setTag:thisrow];

   }
   if ([items.Type isEqualToString:SETTING])
   {
      retcell = [tableView dequeueReusableCellWithIdentifier:@"setting"];
      UISegmentedControl *setting = (UISegmentedControl *)[retcell viewWithTag:201];
      // set text for segments...
      [setting setTag:thisrow];
   }

return (retcell);
}
4

1 回答 1

0

在我看来,您应该UITableViewCell为您的YESNO单元格和SETTING单元格创建一些子类。然后在 中tableView:cellForRow:atIndexPath:,返回 aYESNOTableViewCell或 a SETTINGTableViewCell。像这样的东西:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
   UITableViewCell *retcell;
   NSInteger thisrow = indexPath.row;

   // retrieve the items which should be printed for this
   // row. set text on widget depending on item type
   OBJ_Items *items = [mCriteria objectForKey:thisrow];

   if ([items.Type isEqualToString:YESNO])
   {
      YESNOTableViewCell yesNoCell = [tableView dequeueReusableCellWithIdentifier:@"yesno"];

      // .. set text for yesno segments
      // the UISegmentedControl will be an IBOutlet @property on YESNOTableViewCell

      // save the row numer to this object's tag so we can retrieve the item object
      // on click
      [yesNoCell.segmentedControl setTag:thisrow];

      // return the yesNoCell
      retcell = yesNoCell;

   }
   if ([items.Type isEqualToString:SETTING])
   {
      SETTINGTableViewCell settingCell = [tableView dequeueReusableCellWithIdentifier:@"setting"];

      // set text for segments...
      // the UISegmentedControl will be an IBOutlet @property on SETTINGTableViewCell

      [settingCell.setting setTag:thisrow];
   }

return (retcell);
}
于 2013-11-04T18:58:39.473 回答