Ios 在UITable View单元格中滚动时自动指定的UISlider值
我正在编写一个类似以下内容的程序: 有一个表视图i,其中有一个自定义表视图单元格。 自定义视图单元上有一个UISlider、标签和按钮 现在的问题是,当我滑动单元格0的UISlider时,单元格12(或更高的单元格)的UISlider会自动指定单元格0的UISlider值(感谢ARC..!!) 现在任何人都有了一个解决方案,这样后面的单元格的UISlider不会改变,而我会改变上面单元格的值 另外,当我在单元格0处指定UiSlider值并上下滚动时,随机单元格的UiSlider值会自动发生变化 项目的Google驱动链接: 我使用的是xCode 5和iOS SDK 7 谢谢你的阅读 编辑: cellForRowAtIndexPath方法Ios 在UITable View单元格中滚动时自动指定的UISlider值,ios,objective-c,uitableview,uislider,Ios,Objective C,Uitableview,Uislider,我正在编写一个类似以下内容的程序: 有一个表视图i,其中有一个自定义表视图单元格。 自定义视图单元上有一个UISlider、标签和按钮 现在的问题是,当我滑动单元格0的UISlider时,单元格12(或更高的单元格)的UISlider会自动指定单元格0的UISlider值(感谢ARC..!!) 现在任何人都有了一个解决方案,这样后面的单元格的UISlider不会改变,而我会改变上面单元格的值 另外,当我在单元格0处指定UiSlider值并上下滚动时,随机单元格的UiSlider值会自动发生变
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableCell = @"Cell";
CustomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableCell];
if (cell == nil) {
cell = [[CustomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableCell];
}
NSString *strName = [NSString stringWithFormat:@"Cell : %d",indexPath.row];
// NSLog(@"strName :%@ , SliderValue : %d",strName , (int)cell.mySlider.value);
for (int i = 0; i < arrSlider.count; i++) {
NSString *strTag = [NSString stringWithFormat:@"%@",[[arrSlider objectAtIndex:i]valueForKey:@"tag"]];
NSString *myIndexPath = [NSString stringWithFormat:@"%d",indexPath.row];
if([strTag isEqualToString:myIndexPath])
{
NSString *strValue = [NSString stringWithFormat:@"%@",[[arrSlider objectAtIndex:i]valueForKey:@"value"]];
cell.mySlider.value = [strValue floatValue];
NSLog(@"Tag Value : %@ , value %f", strTag , [strValue floatValue]);
}
}
[cell.btnCell setTitle:strName forState:UIControlStateNormal];
cell.btnCell.tag = indexPath.row;
cell.mySlider.tag = indexPath.row;
[cell.mySlider addTarget:self action:@selector(customSliderValue:) forControlEvents:UIControlEventValueChanged];
[cell.btnCell addTarget:self action:@selector(customeBtnClicked:) forControlEvents:UIControlEventTouchDown];
return cell;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*simpleTableCell=@“Cell”;
CustomeTableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleTableCell];
如果(单元格==nil){
cell=[[CustomeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableCell];
}
NSString*strName=[NSString stringWithFormat:@“单元格:%d”,indexath.row];
//NSLog(@“strName:%@,SliderValue:%d”,strName,(int)cell.mySlider.value);
对于(int i=0;i
您不需要检查(设置)一个单元格的所有数据源,我的意思是,在单元格内不需要for循环forrowatindexpath
只需将其删除,即可正常工作。请尝试以下代码它对我有效:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = [NSString stringWithFormat:@"%d",indexPath.row];
CustomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[CustomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// Write your rest code here
return cell;
}
使用
NSMutableDictionary
保存滑块的值,然后从cellforrowatinexpath
方法更新滑块。我正在发布更改,只需在项目中进行更改
在ViewCOntroller.h
文件中
#import <UIKit/UIKit.h>
#import "CustomeTableViewCell.h"
@interface ViewController : UIViewController <UITableViewDataSource ,UITableViewDelegate,SliderDelegate>//confirms to delegate
{
//NSArray *tableList;
UITableView *mytableview;
NSInteger SliderChangeValue;
}
@property (strong , nonatomic) IBOutlet UIView *tableDemo;
@property (strong , nonatomic) NSMutableArray *arrSlider;
@property (strong, nonatomic) NSMutableDictionary *sliderDicValues; //add a mutable dictionary
@property (weak, nonatomic) IBOutlet UITableView *myTableView;//add outlet to tableview
@end
#import <UIKit/UIKit.h>
//add a custom delegate
@protocol SliderDelegate<NSObject>
- (void)sliderChanged:(id)self;
@end
@interface CustomeTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *myCellLabel;
@property (weak, nonatomic) IBOutlet UISlider *mySlider;
@property (weak, nonatomic) IBOutlet UIButton *btnCell;
@property (weak, nonatomic) id <SliderDelegate>sliderDelegate;
- (IBAction)sliderValuechanged:(UISlider *)sender;
@end
#import "CustomeTableViewCell.h"
@implementation CustomeTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)sliderValuechanged:(UISlider *)sender
{
self.myCellLabel.text = [NSString stringWithFormat:@"%d",(NSInteger)sender.value];
//call the custom delegate each time when slider is slided
if([_sliderDelegate respondsToSelector:@selector(sliderChanged:)])
{
[_sliderDelegate sliderChanged:self]; //passing the entire cell itself
}
}
@end
在CustomeTableViewCell.h
文件中
#import <UIKit/UIKit.h>
#import "CustomeTableViewCell.h"
@interface ViewController : UIViewController <UITableViewDataSource ,UITableViewDelegate,SliderDelegate>//confirms to delegate
{
//NSArray *tableList;
UITableView *mytableview;
NSInteger SliderChangeValue;
}
@property (strong , nonatomic) IBOutlet UIView *tableDemo;
@property (strong , nonatomic) NSMutableArray *arrSlider;
@property (strong, nonatomic) NSMutableDictionary *sliderDicValues; //add a mutable dictionary
@property (weak, nonatomic) IBOutlet UITableView *myTableView;//add outlet to tableview
@end
#import <UIKit/UIKit.h>
//add a custom delegate
@protocol SliderDelegate<NSObject>
- (void)sliderChanged:(id)self;
@end
@interface CustomeTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *myCellLabel;
@property (weak, nonatomic) IBOutlet UISlider *mySlider;
@property (weak, nonatomic) IBOutlet UIButton *btnCell;
@property (weak, nonatomic) id <SliderDelegate>sliderDelegate;
- (IBAction)sliderValuechanged:(UISlider *)sender;
@end
#import "CustomeTableViewCell.h"
@implementation CustomeTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)sliderValuechanged:(UISlider *)sender
{
self.myCellLabel.text = [NSString stringWithFormat:@"%d",(NSInteger)sender.value];
//call the custom delegate each time when slider is slided
if([_sliderDelegate respondsToSelector:@selector(sliderChanged:)])
{
[_sliderDelegate sliderChanged:self]; //passing the entire cell itself
}
}
@end
希望这对你有帮助:) 如何在
表视图:CellForRowatineXpath:
中声明滑块?我已使用CellForRowatineXpath方法编辑了我的问题。您的滑块数组似乎在0和12个索引处包含相同的对象。如何填充它?什么类型的NSArray
是arrslaider
?它似乎包含一个NSDictionary
。你能给我们它的结构(键、值)或者它的一个例子吗?我也尝试过删除它,但它不起作用。