for CustomeTableViewCell.h
//
// CustomeTableViewCell.h
// Slider Program
//
// Created by Naeem Shaikh on 21/08/14.
// Copyright (c) 2014 KIintu Designs Pvt. Ltd. All rights reserved.
//
#import <UIKit/UIKit.h>
@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
for CustomeTableViewCell.m
//
// CustomeTableViewCell.m
// Slider Program
//
// Created by Naeem Shaikh on 21/08/14.
// Copyright (c) 2014 KIintu Designs Pvt. Ltd. All rights reserved.
//
#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
for viewcontroller.h
#import <UIKit/UIKit.h>
#import "CustomeTableViewCell.h"
@interface ViewController : UIViewController <UITableViewDataSource , UITableViewDelegate>{
//NSArray *tableList;
UITableView *mytableview;
int SliderChangeValue;
}
@property (strong , nonatomic) IBOutlet UIView *tableDemo;
@property (strong , nonatomic) NSMutableArray *arrSlider;
@property (strong, nonatomic) NSMutableDictionary *sliderDicValues;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;//add outlet to tableview
@end
for viewcontroller.m
//
// ViewController.m
// Slider Program
//
// Created by Naeem Shaikh on 21/08/14.
// Copyright (c) 2014 KIintu Designs Pvt. Ltd. All rights reserved.
//
#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];
//[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 50;
}
-(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;
}
- (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
这是整个应用的链接
https://www.mediafire.com/folder/1d4ktomjytb818l,8zcg0zbymee57z8/shared
它是tableview中的滑块演示解决方案。