【问题标题】:Segmentcontrol in tableviewcelluitableviewcell 中的 Uisegmentedcontrol
【发布时间】:2013-11-30 02:24:55
【问题描述】:

我得到了这个表格视图,每个单元格中都有分段控件。我从我的数据库中获取了一些数据,这些数据放入了单元格分段控件中。但是如何设置没有选择段?如果在数据库中找到注释,则返回 -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];


【问题讨论】:

  • 嗨!我认为 segmentedControl 不能没有选定的段
  • 什么? Segmentedcontrols 是用来控制段的吗?不会有 segmentedControl.selectedSegmentIndex = -1;如果不可能..如果你知道我需要做什么,请说。

标签: ios uitableview uisegmentedcontrol


【解决方案1】:

文档说明:

selectedSegmentIndex
默认值为UISegmentedControlNoSegment(未选择段),直到用户触摸段。将此属性设置为 -1 以关闭当前选择。

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

【讨论】:

  • 那会是什么?我在上面的代码中声明了与分段控件有关的所有内容:(
  • 您是否确保您的表格视图单元格被正确回收? IE。你有没有在你的单元子类中实现-prepareForReuse,并确保它的状态被重置?
  • 我可以看到您没有使用单元格子类,并且每次返回单元格时都将分段控件添加为新的子视图。使用单元格重用,这可能会导致您的单元格同时在视图上具有多个分段控件实例。
  • 我建议将表格视图单元格子类化,将分段控件添加为属性,并将其添加到视图中,并控制其状态,而不是每次都创建一个新的。
  • 嗨 :) 我对这个分段控件非常陌生 :( 当您说“将分段控件添加为属性并将其添加到视图中”时,您的意思是我需要拖动分段控件进入我的视图控制器,然后声明为属性?因为我已经尝试过了,每次打开视图时,它都会崩溃:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
  • 2011-07-14
相关资源
最近更新 更多