在UITableView中UISegmentedControl的奇怪行为

时间:2012-03-29 12:46:56

标签: iphone objective-c uitableview uisegmentedcontrol

我正在使用使用ELCTextfieldCells的UITableView制作应用。对于某些单元格,我在它上面设置了一个UISegmentedControl并禁用了UITextField。 “一张图片胜过千言万语”,所以这里是我滑到UITableView时得到的截图:

enter image description here

在从屏幕上移动该单元并将其放回(基本上重新加载)后,我得到以下内容:

enter image description here

所以有两个问题:

  • 第一次出现视图时,UISegmentedControl不可见;
  • UISegmentedControl的值写在顶部的单元格中;

一些代码无论如何影响这些细胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[self.tableViewCellValues objectAtIndex:indexPath.row]rangeOfString:@"Gender"].location != NSNotFound || [[self.tableViewCellValues objectAtIndex:indexPath.row]rangeOfString:@"Still Active Employee"].location != NSNotFound) {
    static NSString *CellIdentifier = @"CellWithSegmentedControl";
    ELCTextfieldCell *cell = (ELCTextfieldCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[ELCTextfieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    [self configureCell:cell atIndexPath:indexPath];
    UISegmentedControl *segmentedControl;
    if ([cell.leftLabel.text rangeOfString:@"Gender"].location != NSNotFound) {
        segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Male", @"Female", nil]];
    } else {
        segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Yes", @"No", nil]];
    }
    segmentedControl.frame = CGRectMake(215, 6, cell.rightTextField.frame.size.width, cell.rightTextField.frame.size.height - 10);
    segmentedControl.tag = 26;
    [segmentedControl addTarget:self action:@selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
    UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
    [cell addSubview:segmentedControl];
    [cell bringSubviewToFront:segmentedControl];
    cell.rightTextField.text = @"";
    cell.rightTextField.enabled = NO;
    return cell;
  }
  //checks and other stuff for other cells
}

-(void)segmentedControlHasChangedValue:(id)sender {
  NSLog(@"Nothing implemented here yet");
}

如何解决问题的任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

您正在提供cell.rightTextField.frame.size.height - 10

可能就是为什么它会上升,你检查了高度吗?