2015-04-04 18 views
0

大家好,我是iOS編程新手!我有一個自定義表格視圖控制器與自定義表格視圖單元格!其中一個細胞有一個uislider和一個標籤!我想在滑塊更改值時更改標籤文本!這是我的代碼:在桌子上的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]; 
} 

}

+0

問題是,當我運行應用程序崩潰並說' - [UITableViewCell滑塊]:無法識別的選擇器發送到實例 – 2015-04-04 15:59:46

+0

您是否已將滑塊連接到自定義單元格?清理項目並重新構建。我昨天也面臨同樣的問題。 – Jassi 2015-04-04 17:18:23

+0

你爲這個表視圖註冊單元類嗎?像 registerClass:forCellReuseIdentifier:?無論如何,你應該使用一個單元格類型的重用標識符,而不是這樣做:[cellInfo objectForKey:@「cell」](實際上取決於indexPath.row) – Neru 2015-04-04 17:47:10

回答

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)

可能有一些錯別字和/或缺少分號,因爲我在沒有編譯器的情況下編寫了它。

+0

非常感謝!不幸的是在這個領域仍然是新手!現在我會按照你告訴我的方式嘗試,我會讓你知道!再次感謝 – 2015-04-05 14:14:32

+0

@Neru如果我有3個部分怎麼辦? – 2016-11-14 12:42:16

0

我這樣做更容易。 Apple寫道您可以對靜態行使用IBActions。 (你可以在The Technique for Static Row Content中看到它here。但是我已經和動態的細胞測試了它的iOS 9,它只是工作:)

起初 - 自定義單元格與IBAction爲

@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 

二 - 的TableView委託

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    SliderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuseIdentifierSliderCell]; 
    cell.slider.value = 142; 
    cell.sliderValueLabel.text = @"1_142"; 
     return cell; 
} 

第三 - 運行您的應用程序nd享受你自己;)