我正在创建一个应用程序,我需要在动态 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
。由于出队过程,您可能会遇到一些奇怪的事情。
我建议您执行以下操作:
- 使用相应的
.h
、.m
文件和.xib
文件创建自定义表格 View 单元 - 在您的自定义单元格中,您可以添加任何您喜欢的 View 以及您喜欢的任意多个 View 。
- 确保在
.h
文件中创建类型为UIScrollView
的property
并将其链接到界面生成器到自定义单元格的 slider ,调用属性slider
。 - 现在在您创建表格的主视图 Controller 中,确保您有一个
NSMutableArray
并将其命名为类似sliderValuesArray
的名称,它可以存储您的所有 slider 值每个细胞。您要确保单元格数等于sliderValuesArray
中的元素数。 - 然后您可以在您的
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/