uitablegcell中的uisegmentedcontrol

时间:2013-11-29 13:21:49

标签: ios uitableview uisegmentedcontrol

我在每个单元格中都有分段控件。我从我的数据库中获取了一些数据,我将其放入细胞分段控制中。但是如何设置不选择段?如果在数据库中找到注意,则返回-1。我尝试了:segmentedControl.selectedSegmentIndex = -1;,但没有运气。看起来它设置了segmentedControl.selectedSegmentIndex = 0;,无论如何都选择了索引为0的过程段(即使我得到了结果)。查看截图以了解:)

顺便说一下,我可以在每个分段控件中选择尽可能多的段。 为什么这可能?另一个问题会解决这个问题吗? :)

谢谢!

我创建单元格的代码是:


  UITableViewCell *cell = nil;
  cell = [tableView dequeueReusableCellWithIdentifier:@"StateCell"];
  if (cell == nil)
  {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseI dentifier:@"StateCell"];
  }

  UIImage *correctImageGreen = [[UIImage imageNamed:@"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  UIImage *correctImageGul = [[UIImage imageNamed:@"gul.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  UIImage *correctImageRed = [[UIImage imageNamed:@"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

  NSArray *itemArray = [NSArray arrayWithObjects: correctImageGreen, correctImageGul, correctImageRed, nil];
  UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];

 segmentedControl.frame = CGRectMake(308, 7, 150, 28);

 [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

 int newIndexPath = indexPath.row + 1;
 NSString *indexPathKey = [NSString stringWithFormat:@"%i", newIndexPath];


 NSString *selectedState = [statefieldData valueForKey:[NSString stringWithFormat:@"StateField%@", indexPathKey]];
 int selectedIndexInt = [selectedState intValue];

 if (selectedIndexInt == -1) {
     segmentedControl.selectedSegmentIndex = -1;
 } 
 else {
    segmentedControl.selectedSegmentIndex = selectedIndexInt;
 }

 [cell.contentView addSubview:segmentedControl];

 cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

 cell.textLabel.textColor = [UIColor whiteColor];
 cell.selectionStyle = UITableViewCellSelectionStyleNone;
 [self.tableView setBackgroundColor:[UIColor colorWithRed:37 green:37 blue:37 alpha:0]];
 cell.backgroundColor = [UIColor clearColor];


 return cell;
 [[self tableView] reloadData];


Screenshot of my problem

1 个答案:

答案 0 :(得分:2)

文档说明:

  

<强> selectedSegmentIndex
  在用户触摸片段之前,默认值为UISegmentedControlNoSegment(未选择片段)。将此属性设置为-1可关闭当前选择。

如果您设置为-1(UISegmentedControlNoSegment),并且取消选择该细分,那么您还有其他错误,我们看不到。