使用dequeueReusableCellWithIdentifier时分配UItableView单元格标记

时间:2013-11-04 16:59:28

标签: ios uitableview uistoryboard

我在故事板中定义了三个原型单元格,每个单元格中都包含一个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);
}

1 个答案:

答案 0 :(得分:0)

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

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