我正在制定一个看起来像这样的程序:
有一个表视图i,有一个自定义表视图单元格。 自定义视图单元格上有一个UISlider,Label和Button
现在的问题是当我滑动单元格的UISlider:0而不是单元格中的UISlider时:12(或更高版本的单元格)自动分配单元格:0的UISlider值(感谢ARC .. !!)。
现在任何人都有一个解决方案,以便后一个单元格的UISlider doest在我更改上部单元格的值时更改。
P.S。当我在Cell上分配UiSlider值时:0并向上和向下滚动它是自动随机的Cell的UISlider值正在改变。
项目的Google云端硬盘链接: Slider Program
我正在使用xCode 5和iOS SDK 7。
感谢阅读。
编辑: cellForRowAtIndexPath方法
-(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;
}
答案 0 :(得分:3)
使用NSMutableDictionary
保存滑块的值,然后从我发布更改的cellForRowAtIndexPath
方法更新它,只需在项目中进行更改
ViewCOntroller.h
<{1>}文件中的
#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
<{1>}文件中的
ViewController.m
<{1>}文件中的
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize arrSlider;
@synthesize sliderDicValues;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// tableList = [NSArray arrayWithObjects:
// @"Cell 1",@"Cell 2",@"Cell 3",@"Cell 4",@"Cell 5",
// @"Cell 6",@"Cell 7",@"Cell 8",@"Cell 9",@"Cell 10",
// @"Cell 11",@"Cell 12",@"Cell 13",@"Cell 14",@"Cell 15",
// @"Cell 16",@"Cell 17",@"Cell 18",@"Cell 19",@"Cell 20",
// nil];
arrSlider = [[NSMutableArray alloc]init];
sliderDicValues = [[NSMutableDictionary alloc]init]; //initilise an mutable dictionary
//[mytableview registerClass:[CustomeTableViewCell class] forCellReuseIdentifier:@"Cell"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//[tableList count]
return 15;
}
-(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];
}
if([self.sliderDicValues objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]]) //check if there is any slided value is present
{
NSNumber *value = [self.sliderDicValues objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]];
[cell.mySlider setValue:value.integerValue]; //set the slider value
[cell.myCellLabel setText:[NSString stringWithFormat:@"%d",value.integerValue]];//and also label
}
else //set to default values
{
[cell.mySlider setValue:(NSInteger)0];
[cell.myCellLabel setText:@"label"];
}
//add a single target don't add double target to slider
cell.sliderDelegate = self;//set the delegate
return cell;
/*
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]);
}
if ([strTag isEqual:myIndexPath]) {
//NSString *strValue = [NSString stringWithFormat:@"%@",[[arrSlider objectAtIndex:i]objectForKey:@"value"]];
//NSLog(@"%@",strValue);
NSLog(@"Hello");
//cell.mySlider.value =
}
}
[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];
*/
}
//delegate method called from custom cell
- (void)sliderChanged:(CustomeTableViewCell *)cell
{
NSIndexPath *path = [_myTableView indexPathForCell:cell]; //get the indexpath
if(path)//check if valid path
{
SliderChangeValue = cell.mySlider.value;
[self.sliderDicValues setObject:[NSNumber numberWithInt:SliderChangeValue] forKey:[NSString stringWithFormat:@"%d",path.row]]; //set the value in the dictionary later used in the cellForRowAtIndexPath method
}
// SliderChangeValue = (int)sender.value;
NSLog(@"%d",SliderChangeValue);
}
//dont use it
-(void)customSliderValue:(UISlider *)sender{
// NSString *value =[NSString stringWithFormat:@"%d" ,(int)sender.value];
// NSString *tag = [NSString stringWithFormat:@"%d", (int)sender.tag];
//
// NSLog(@"%@ %@",value , tag);
//
// [self.dicSilder setObject:value forKey:@"value"];
// [self.dicSilder setObject:tag forKey:@"tag"];
//
// [self.arrSlider addObject:self.dicSilder];
// NSLog(@"%@",self.arrSlider);
SliderChangeValue = (int)sender.value;
NSLog(@"%d",SliderChangeValue);
}
//this is also put a delegate from the cell like slider , just add the another method in the protocol and perform action, if u don't get just comment i will update the code and u hav t modify this method according to your requirement
-(void)customeBtnClicked:(UIButton *)sender
{
NSString *value =[NSString stringWithFormat:@"%d" ,SliderChangeValue];
NSString *tag = [NSString stringWithFormat:@"%d", sender.tag];
//NSLog(@"%@ %@",value,tag);
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
[dic setObject:value forKey:@"value"];
[dic setObject:tag forKey:@"tag"];
//NSLog(@"%@",dic);
[arrSlider addObject:dic];
NSLog(@"%@",arrSlider);
NSString *sliderTagAtIndexPath = @"";
//NSString *sliderValueAtindexPath = @"";
for (int i = 0; i < arrSlider.count; i++) {
NSString *strTag = [NSString stringWithFormat:@"%@",[[arrSlider objectAtIndex:i]valueForKey:@"tag"]];
if([strTag isEqualToString:tag])
{
//NSString *strValue = [NSString stringWithFormat:@"%@",[[arrSlider objectAtIndex:i]valueForKey:@"value"]];
sliderTagAtIndexPath = strTag;
//sliderValueAtindexPath = strValue;
}
}
UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"Clicked"
message:[NSString stringWithFormat:@"Cell : %@ Value: %d", sliderTagAtIndexPath ,SliderChangeValue]
//message:[NSString stringWithFormat:@"Cell : %@ Value: %@", sliderTagAtIndexPath ,sliderValueAtindexPath]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[myAlertView show];
}
@end
希望这有助于你......:)
答案 1 :(得分:0)
您不需要检查(设置)Cell的所有数据源,我的意思是, cellForRowAtIndexPath
中不需要for循环。 只需删除它即可。
答案 2 :(得分:0)
试试这个适用于我的代码:
- (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;
}