1

我是目标 C 的新手。我想更改放置在单元格中的标签相对于放置在同一单元格中的 UIslider 的背景颜色渐变。随着滑块从左向右移动,背景颜色的渐变应该会发生变化。谁能指导我怎么做???

4

1 回答 1

0

此代码将根据滑块值的变化更改 UILabel 背景颜色的 alpha。

首先,您需要在自定义单元格中声明滑块。如下所述。

在 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 回答