ios - 动态 UITableView 中的 UISlider

标签 ios objective-c uitableview uislider

我正在创建一个应用程序,我需要在动态 UITableView 中添加一堆 slider 。我像这样添加了 slider :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    UISlider *slider = [[UISlider alloc]init];
    //ALL OTHER CODE
    [cell addSubview:slider];
    return cell;
}

现在 slider 已添加到 UITableView,但如果我更改了第一个 slider 的值,另一个 slider 也会随之更改。我知道这与单元格出列有关,但我该如何修复它?

编辑:

我试过@daveMack 这样的回答:

自定义单元格.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.checkBox = [[M13Checkbox alloc]initWithTitle:@"Checkbox!"];
        self.checkBox.checkAlignment = M13CheckboxAlignmentLeft;
        [self addSubview:self.checkBox];
    }
    return self;
}

索引路径处行的单元格:

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

    cell = [self.tableView dequeueReusableCellWithIdentifier:@""];
    if (!cell) {
        cell = [[CheckboxCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        return cell;
    }
    return cell;
}

最佳答案

不不不!我不建议您将这样的 View 添加到您的 UITableViewCell。由于出队过程,您可能会遇到一些奇怪的事情。

我建议您执行以下操作:

  1. 使用相应的 .h.m 文件和 .xib 文件创建自定义表格 View 单元
  2. 在您的自定义单元格中,您可以添加任何您喜欢的 View 以及您喜欢的任意多个 View 。
  3. 确保在 .h 文件中创建类型为 UIScrollViewproperty 并将其链接到界面生成器到自定义单元格的 slider ,调用属性 slider
  4. 现在在您创建表格的主视图 Controller 中,确保您有一个 NSMutableArray 并将其命名为类似 sliderValuesArray 的名称,它可以存储您的所有 slider 值每个细胞。您要确保单元格数等于 sliderValuesArray 中的元素数。
  5. 然后您可以在您的 cellForRowAtIndexPath 委托(delegate)方法中执行类似的操作:

在您的 cellForRowAtIndexPath 方法中执行如下操作:

myCustomCell.slider.maximumValue = 100;
myCustomCell.slider.minimumValue = 1;
myCustomCell.slider.continuous = TRUE;
//set a method which will get called when a slider in a cell changes value
[myCustomCell.slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];

//Keep a reference to each slider by assigning a tag so that we can determine 
//which slider is being changed
myCustomCell.slider.tag = indexPath.row;

//Grab the value from the sliderValuesArray and set the slider knob to that position
myCustomCell.slider.value = [[sliderValuesArray objectAtIndex:indexPath.row] intValue];

现在在您的 sliderChanged 方法中您可以这样做:

-(void)sliderChanged:(UISlider)sender{
    //Grab the slider value, it needs to be converted to an NSNumber so that we can
    //store it successfully as an object in our sliderValuesArray
    NSNumber sliderValue = [NSNumber numberWithInt:sender.value];
    //This is how we determine which position in our slidersArray we want to update,
    //Based on the tag we set our slider view on initialisation in our cellForRowAtIndexPath
    int cellPosition = sender.tag;

    //Use the cellPosition to update the correct number in our sliderValuesArray
    //with the sliderValue retrieved from the slider that the user is sliding
    [sliderValuesArray replaceObjectAtIndex:cellPosition withObject:sliderValue];
}

关于ios - 动态 UITableView 中的 UISlider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21470040/

相关文章:

iOS位置转换

ios - A/B 测试平台如何即时替换 Objective-C Assets ?

ios - 显示大图像文件时在 viewDidAppear 之后调用 DidRecieveMemoryWarning

ios - 全局设置按钮的背景颜色,UITableView 中的辅助按钮除外

添加披露指示器时 ios8 单元格约束中断

ios - iOS 应用程序中的核心数据和文本文件

ios - subview Controller 必须有一个共同的父 View Controller ?

ios - 核心剧情: Multiple colors for scatter plot fill area

objective-c - UITableViewCell 上的 SIG_ABRT 删除

ios - 如何在表格 View 中正确设置动态标题 View ?