Ios 动态UITableView中的UISlider
我正在创建一个应用程序,其中我需要在动态UITableView中添加一组滑块。我添加了如下滑块:Ios 动态UITableView中的UISlider,ios,objective-c,uitableview,uislider,Ios,Objective C,Uitableview,Uislider,我正在创建一个应用程序,其中我需要在动态UITableView中添加一组滑块。我添加了如下滑块: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPat
-(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;
}
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];
现在,滑块已添加到UITableView,但如果我更改了第一个滑块的值,另一个滑块也会随之更改。我知道这与单元格的消隐有关,但如何修复它
编辑:
- (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;
}
我试过@daveMack这样回答:
-(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;
}
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];
CustomCell.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;
}
不久前我也遇到了同样的问题。我提出的解决方案是将UITableViewCell子类化。然后我在子类的init方法中添加了slider,并通过一个属性将其公开 现在,如果只想更改一个滑块的值,可以执行以下操作:
[cell slider]setValue:(someValue)];
不久前我也遇到了同样的问题。我提出的解决方案是将UITableViewCell子类化。然后我在子类的init方法中添加了slider,并通过一个属性将其公开 现在,如果只想更改一个滑块的值,可以执行以下操作:
[cell slider]setValue:(someValue)];
不,不,不!我不建议您在
UITableViewCell
中添加这样的视图。由于排队过程的原因,你可能会经历一些令人生厌的事情
我建议你做以下几点:
.h
、.m
文件和.xib
文件创建自定义表格视图单元格.h
文件中创建类型为UIScrollView
的属性
,并将其链接到界面生成器到自定义单元格的滑块,调用属性滑块
NSMutableArray
,并将其命名为sliderValuesArray
,它可以存储每个单元格的所有滑块值。您希望确保单元格的数量等于sliderValuesArray
中的元素数量cellforrowatinexpath
delegate方法中执行类似操作:cellforrowatinexpath
方法中,执行以下操作:
-(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;
}
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];
}
不,不,不!我不建议您在
UITableViewCell
中添加这样的视图。由于排队过程的原因,你可能会经历一些令人生厌的事情
我建议你做以下几点:
.h
、.m
文件和.xib
文件创建自定义表格视图单元格.h
文件中创建类型为UIScrollView
的属性
,并将其链接到界面生成器到自定义单元格的滑块,调用属性滑块
NSMutableArray
,并将其命名为sliderValuesArray
,它可以存储每个单元格的所有滑块值。您希望确保单元格的数量等于sliderValuesArray
中的元素数量cellforrowatinexpath
delegate方法中执行类似操作:cellforrowatinexpath
方法中,执行以下操作:
-(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;
}
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];
}
这很好用,但当我滚动完成后又返回时,它就失去了价值。@AbdullahShafique是的,伙计,看看我的答案,兄弟。你将看到如何纠正它。这很好,但当我滚动完成后,它就会失去它的价值。@AbdullahShafique是的,伙计,看看我的答案,兄弟。您将看到如何更正它。Great@AbdullahShafique:)由于单元格是可重用的,是否有可能每次调用
cellForRowAtIndexPath
时都添加滑块的目标和操作?Great@AbdullahShafique:)因为单元格是可重用的,是否每次调用cellForRowAtIndexPath
时都会添加滑块的目标和操作?