Uislider在tableView Cell中

时间:2015-04-04 15:58:11

标签: objective-c

大家好,我是iOS编程的新手!我有一个自定义表视图控制器与自定义表视图单元格!其中一个细胞有一个uislider和一个标签!我想在滑块更改值时更改标签文字!这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



NSDictionary *cellInfo = [[self.sections objectAtIndex:currentTab] objectAtIndex:indexPath.row];
HLNotificheCell *cell = [tableView dequeueReusableCellWithIdentifier:[cellInfo objectForKey:@"cell"] forIndexPath:indexPath];

UIImageView *radioIndicator = (UIImageView *)[cell.contentView viewWithTag:200];

radioIndicator.image = (currentBullet != indexPath.row) ? [UIImage imageNamed:@"RadioOff"] : [UIImage imageNamed:@"RadioOn"];

UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:@"NewsSeparetor.png"];
cell.backgroundView = av;


cell.slider.maximumValue = 100;
cell.slider.minimumValue = 1;
cell.slider.continuous = TRUE;
//set a method which will get called when a slider in a cell changes value
[cell.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
cell.slider.tag = indexPath.row;

//Grab the value from the sliderValuesArray and set the slider knob to that position






return cell;

}

-(void)sliderChanged:(UISlider*)sender{
HLNotificheCell *cell = [[HLNotificheCell alloc]init];
if (sender == cell.slider) {
    cell.label.text = [NSString stringWithFormat:@"%0.3f", cell.slider.value];
}

}

2 个答案:

答案 0 :(得分:1)

实际上代码中存在很多的不良做法。请让我解释一下。

让我们从您的HLNotificheCell课程开始。我认为头文件应该如下所示:

#import <UIKit/UIKit.h>

#define HLNotificheCellIdentifier @"HLNotificheCellIdentifier"

@interface HLNotificheCell : UITableViewCell

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier;

@property (strong, nonatomic) UISlider *slider;
@property (strong, nonatomic) UIImageView *radioIndicator;

@end

和实施文件:

#import "HLNotificheCell.h"

@implementation HLNotificheCell

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    if (self) {
        _slider = [[UISlider alloc] init];
        _slider.maximumValue = 100;
        _slider.minimumValue = 1;
        _slider.continuous = YES; //YES is more natural in objc rather than TRUE.
        [self addSubview: _slider];

        _radioIndicator = [[UIImageView alloc] init];
        [self addSubview:_radioIndicator];

        UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
        av.backgroundColor = [UIColor clearColor];
        av.opaque = NO;
        av.image = [UIImage imageNamed:@"NewsSeparetor.png"];
        self.backgroundView = av;

        //it's better to use built-in textLabel instead of creating your own. Trust me when you will have 20 different customized cells you will get lost with their names.
        self.textLabel.textColor = [UIColor redColor];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    // layout your self.slider and self.radioIndicator here or use xib for it.
    // e.g. this will layout slider to fit whole cell:
    self.slider.frame = self.bounds;
}

@end

好的,现在转到cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // try to dequeue cell if exist
    HLNotificheCell *cell = (HLNotificheCell *)[tableView dequeueReusableCellWithIdentifier:HLNotificheCellIdentifier];

    // if doesn't, create new one.
    if (!cell) { // is enough to set slider target only once when cell is created. When reuse is not needed.
        cell = [[HLNotificheCell alloc] initWithReuseIdentifier:HLNotificheCellIdentifier];
        [cell.slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
    }

    //set image as you wish:
    cell.radioIndicator.image = (currentBullet != indexPath.row) ? [UIImage imageNamed:@"RadioOff"] : [UIImage imageNamed:@"RadioOn"];

    //Keep a reference to each slider by assigning a tag so that we can determine
    //which slider is being changed
    cell.slider.tag = indexPath.row;

     //Grab the value from the sliderValuesArray and set the slider knob to that position

    NSNumber *sliderValue = sliderValuesArray[indexPath.row];
    [cell.slider setValue:sliderValue.floatValue animated:NO]

    return cell;
}

sliderChanged:方法:

-(void)sliderChanged:(UISlider*)sender{
    // You cannot do this:
    // HLNotificheCell *cell = [[HLNotificheCell alloc]init];
    // because you have to restore reference from sender.tag as you wrote in cellForRowAtIndexPath method:

    HLNotificheCell *cell = (HLNotificheCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:slider.tag inSection:0]] // I assume you have only 1 section
    cell.textLabel.text = [NSString stringWithFormat:@"%0.3f", cell.slider.value]; 
    //save new value to the sliderValuesArray
    self.sliderValuesArray[indexPath.row] = @(cell.slider.value);
}

假设:

  • 当您使用此部分代码时,请不要使用registerClass:forCellReuseIdentifier:

  • 您的sliderValuesArray类似NSMutableArray类。

  • sliderValuesArray已初始化,其大小与单元格数相同,如:

    self.sliderValuesArray = [[NSMutableArray alloc] initWithCapacity:<#numberOfCels#>];
    for (int i = 0; i < sliderValuesArray.count; i++) {
        sliderValuesArray[i] = @(0);
    }
    
  • 您的表格视图只包含一种类型的单元格(HLNotificheCell)

可能存在一些拼写错误和/或缺少分号,因为我在没有编译器的情况下编写了它。

答案 1 :(得分:0)

我这样做更容易。 Apple写道,您可以将IBActions用于静态行。 (您可以在r.GetType("FileDialogNative.IFileDialog"); 中了解here。但我已经在iOS 9上使用动态单元格对其进行了测试,它只是起作用:)

  

首先 - 使用IBAction的自定义单元

The Technique for Static Row Content
  

第二 - TableView委托

@interface SliderTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *sliderValueLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@end

@implementation SliderTableViewCell

- (void)awakeFromNib {
    self.slider.minimumValue = 1;
    self.slider.maximumValue = 1000;
}

- (IBAction)sliderValueChanged:(id)sender {
    self.sliderValueLabel.text = [NSString stringWithFormat:@"1_%.f", self.slider.value];
}
@end
  

第三 - 运行你的应用并享受自己;)