1

私は目的の C を初めて使用します。同じセルに配置された UIslider に関して、セルに配置されたラベルの背景色のグラデーションを変更したいと考えています。スライダーが左から右に移動すると、背景色のグラデーションが変化します。誰かが私にそれを行う方法を教えてもらえますか???

4

1 に答える 1

0

このコードは、スライダーの値の変更に応じて UILabel の背景色のアルファを変更します。

まず、カスタム セルでスライダーを宣言する必要があります。以下に説明するように。

SignUpCustomCell.h セルでスライダーを宣言します。

#import <UIKit/UIKit.h>
@interface SignUpCustomCell : UITableViewCell {
    UISlider* slider;
    UILabel *lblLeft;
}
@property(nonatomic,retain) UISlider* slider;
@property(nonatomic,retain)     UILabel *lblLeft;
@end

SignUpCustomCell.m ファイルにメモリを割り当てます。

#import "SignUpCustomCell.h"
@synthesize slider,lblLeft;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {

 slider=[[UISlider alloc]initWithFrame:CGRectMake(150, 5, 100, 25)];
    [slider setValue:0.0];
    slider.minimumValue=0;
    slider.maximumValue=1;
    [self.contentView addSubview:self.slider];

     self.lblLeft = [[UILabel alloc]init];
    self.lblLeft.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
    self.lblLeft.textColor = [UIColor colorWithRed:135.0/255.0 green:135.0/255.0 blue:135.0/255.0 alpha:1.0];
    self.lblLeft.backgroundColor = [UIColor greenColor];
    [self.lblLeft setTextAlignment:UITextAlignmentLeft];
    [self.contentView addSubview:self.lblLeft];
    [self.lblLeft release];



}
return self;
}

カスタムセルを作成した後、必要な場所で使用します。

SignupViewController.m でカスタム セルを使用するには。次の手順が必要です。

#import "SignUpCustomCell.h"

次に、cellForRowAtIndexPath にコードを記述します。

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

static NSString *CellIdentifier = @"Cell";

SignUpCustomCell *cell = (SignUpCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[SignUpCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}
cell.lblLeft.text=@"test app";
cell.slider.tag=indexPath.row;
[cell.slider addTarget:self action:@selector(sliedervalue:) forControlEvents:UIControlEventValueChanged];
return cell;
}

-(void)sliedervalue:(id)sender
{
UISlider* sl=(UISlider*)sender;

NSLog(@"sl=%d",sl.tag);
NSLog(@"sl_value=%f",sl.value);

NSIndexPath *indexPath=[NSIndexPath indexPathForRow:sl.tag inSection:0] ;
SignUpCustomCell *cell = (SignUpCustomCell*)[tblSignup cellForRowAtIndexPath:indexPath];

cell.lblLeft.alpha= sl.value;

}

ご不明な点がございましたらお知らせください。

于 2011-10-07T10:26:54.173 に答える