UITableViewCell中的UISlider移动到其他UITableViewCells

时间:2015-02-15 14:25:21

标签: objective-c uitableview uiviewcontroller uislider

我在UISlider中有一个UITableViewCell。我添加了一些代码,以便它可以扩展或缩回" Landscape"和#34;肖像"模式。但是现在它跳进了其他UITableviewcells。有人有什么想法吗?这是我的代码:

 - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];

    [super viewDidDisappear:animated];
}

- (void)didRotate {
    [[self tableView] reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// Configure the cell...

static NSString *CellIdentifier;

if ((indexPath.section==kSectionSlider) && (indexPath.row==kSettingSlider)){
    CellIdentifier=@"SliderCell";
}
else{
    CellIdentifier=@"Cell";
}

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    if ([CellIdentifier isEqual:@"Cell"]){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else if ([CellIdentifier isEqual:@"SliderCell"]){
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        CGRect frame = CGRectMake(0.0, 0.0, 0.65*tableView.frame.size.width,cell.frame.size.height);
        UISlider *slider = [[UISlider alloc] initWithFrame:frame];
        [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
        slider.continuous = YES;
        slider.value = _value;
        _slider=slider;
        cell.accessoryView = slider;
    }
}

CGRect frame;
frame = tableView.frame;
frame.size.width=0.65*frame.size.width;
_slider.frame=frame;

switch(indexPath.section){
    case kSectionSlider:{
                cell.textLabel.text = [NSString stringWithFormat:@"%.2f“,_value];
        }
    }break;    }

return cell;
}

1 个答案:

答案 0 :(得分:0)

问题是由于这一行:

CGRect frame;
frame = tableView.frame;
frame.size.width=0.65*frame.size.width;
_slider.frame=frame;

我删除了它并在创建UISlider时添加了这一行:

slider.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

现在一切都按预期工作了。感谢您的帮助。