首页 文章

UISlider值在UITable View Cell中滚动时自动分配

提问于
浏览
2

我正在制定一个看起来像这样的程序:

有一个表视图i,有一个自定义表视图单元格 . 自定义视图单元格上有一个UISlider,Label和Button

enter image description here

enter image description here

现在的问题是当我滑动Cell的UISlider:0而不是Cell的UISlider:12(或更高版本的Cell)自动分配Cell:0的UISlider值(感谢ARC ...... !!) .

现在任何人都有一个解决方案,以便后一个单元格的UISlider doest改变,同时我改变上部单元格的值 .

附:当我在Cell上分配UiSlider值时:0并向上和向下滚动它会自动随机Cell的UISlider值正在改变 .

Google Drive项目链接: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;

}

3 回答

  • 0

    使用 NSMutableDictionary 保存滑块的值然后从 cellForRowAtIndexPath 方法更新它我发布更改只需在项目中进行更改

    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
    

    ViewController.m 文件中

    #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
    

    CustomeTableViewCell.h 文件中

    #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
    

    CustomeTableViewCell.m 文件中

    #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
    

    希望这有助于你.. :)

  • 3

    您不需要检查(设置)Cell的所有数据源,我的意思是, No need of for loop inside the cellForRowAtIndexPath . just remove it and will work fine .

  • 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;
    
    
    }
    

相关问题