【发布时间】: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