ios - tableviewcell中的Segment控件

标签 ios uitableview uisegmentedcontrol

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

顺便说一句,我可以在每个分段控件中选择任意数量的分段。 为什么这可能?其他问题会解决这个问题吗? :)

谢谢!

我创建单元格的代码是:


  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

最佳答案

文档指出:

selectedSegmentIndex
The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.

如果您设置 -1 (UISegmentedControlNoSegment),并且没有取消选择该段,那么您还做错了一些我们看不到的事情。

关于ios - tableviewcell中的Segment控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20286816/

相关文章:

ios - Swift:如何从单例类访问 ViewController 中存在的 CollectionView

ios - 自定义 UITableViewCell 中的按钮操作会影响其他单元格

ios - 当我尝试从字典中删除值时,它抛出错误 "Index out of range"

iphone - UISegmentedControl 取消选择(不选择任何段)

ios - TableView 顶部的分段控件,但不在标题 View 中

objective-c - 使用分段控件刷新 UINavigationbar

objective-c - iOS应用程序在后台崩溃并快速重新启动,没有崩溃日志

ios - 继续黑屏。以前从没见过这个

ios - 自定义文本字段

ios - 在 UITableView 中,cellForRowAtIndexPath 忽略前几行