其實有的代碼中的陋習很多。請讓我解釋一下。
讓我們從你的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)
可能有一些錯別字和/或缺少分號,因爲我在沒有編譯器的情況下編寫了它。
問題是,當我運行應用程序崩潰並說' - [UITableViewCell滑塊]:無法識別的選擇器發送到實例 – 2015-04-04 15:59:46
您是否已將滑塊連接到自定義單元格?清理項目並重新構建。我昨天也面臨同樣的問題。 – Jassi 2015-04-04 17:18:23
你爲這個表視圖註冊單元類嗎?像 registerClass:forCellReuseIdentifier:?無論如何,你應該使用一個單元格類型的重用標識符,而不是這樣做:[cellInfo objectForKey:@「cell」](實際上取決於indexPath.row) – Neru 2015-04-04 17:47:10