我得到了这个表格 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];
最佳答案
文档指出:
selectedSegmentIndex
The default value isUISegmentedControlNoSegment
(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/