2012-04-15 81 views
0

在我的UITableViewCell中,我有一个UIButton和一个已被动态添加到单元格中的标签。 我也有一个单元格的背景图像,所以当这个按钮被点击时,我想要单元格的背景来替换另一个图像,或者我希望标签的文本颜色改变为其他类似灰色或蓝色的东西。如何更改单击按钮上的UITableView单元格的背景图像

这可能吗?

下面是NY cellForRowAtIndexPath的代码:使用所述标签作为其按钮被按下的小区的标识符

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     NSArray *subviews = cell.contentView.subviews; 
     for (UIView *vw in subviews) 
     { 
      if ([vw isKindOfClass:[UILabel class]]) 
      { 
       [vw removeFromSuperview]; 
      } 
     } 

    NSArray * temp; 
    NSData * imageData; 
    if (dataToDisplay!=nil) { 
     temp = [dataToDisplay objectAtIndex:indexPath.row]; 
     imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [temp objectAtIndex:4]]]; 
    } 


    playButtonImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"music_player_play_button.png"]]; 
    pauseButtonImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"music_player_pause_button.png"]]; 

    UIButton* preview = [UIButton buttonWithType:UIButtonTypeCustom]; 
    preview.tag = 100 + indexPath.row; 
    [preview addTarget:self 
       action:@selector(playPreview:) 
    forControlEvents:UIControlEventTouchUpInside]; 
    preview.backgroundColor = [UIColor clearColor]; 
    preview.frame = CGRectMake(11,6, 36, 35); 
     [preview setBackgroundImage:playButtonImage.image forState:UIControlStateNormal]; 
    [cell addSubview:preview]; 

    UILabel * songName = [[UILabel alloc]initWithFrame:CGRectMake(60,15, 150, 15)]; 
    songName.tag = 100+indexPath.row; 
    songName.text = [temp objectAtIndex:5]; 
    songName.font = [UIFont boldSystemFontOfSize:12]; 
    songName.backgroundColor = [UIColor clearColor]; 
    [cell addSubview:songName]; 

    UIButton * buy = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [buy addTarget:self 
       action:@selector(buySong:) 
     forControlEvents:UIControlEventTouchUpInside]; 
     [buy setImage:[UIImage imageNamed:@"button_buy.png"] forState:UIControlStateNormal]; 
    buy.tag = 200 + indexPath.row; 
    [buy setTitle:@"Buy" forState:UIControlStateNormal]; 
    buy.titleLabel.font = [UIFont boldSystemFontOfSize:8.0]; 
    buy.frame = CGRectMake(255,9,41, 30); 
    [cell addSubview:buy]; 
    songsResults.tableHeaderView = headerView; 
     UIImage * cellIcon; 
    if (indexPath.row==[dataToDisplay count]-1) { 
     cellIcon = [UIImage imageNamed:[cellViewImages objectAtIndex:1]]; 
    } 
    else { 
     cellIcon = [UIImage imageNamed:[cellViewImages objectAtIndex:0]]; 
    } 
     cell.backgroundView = [[UIImageView alloc]initWithImage:cellIcon]; 

     cell.clipsToBounds = YES; 

    } 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 

回答

2

。这样,你可以决定你需要更改单元格:你应该继承UITableViewCell

-(void)buySong:(id)sender { 
    UIButton *buttonPressed = (UIButton*)sender; 
    NSUInteger rowSelected = buttonPressed.tag - 200; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowSelected inSection:0]; 
    CustomTableViewCell *cellSelected = (CustomTableViewCell)[self.tableView cellForRowAtIndexPath:indexPath]; 
    cellSelected.label.textColor = [UIColor redColor]; 
} 

这样你就可以添加新的属性,并访问他们以后,就像我跟CustomTableViewCell一样。

Here是如何创建自定义UITableViewCell的教程。

干杯

+0

好吧,我明白这一点。但是,我是否需要在这个子类(CustomTableViewCell)中编写任何代码? – Ashutosh 2012-04-15 21:26:56

+0

@Ashutosh我编辑了答案,看看如何创建一个自定义单元格。 – 2012-04-15 21:39:40

+0

这看起来像一个很大的努力,因为一切都已在我的tableview单元格中完成。有没有一种方法,我可以实现这一点,而不需要子类。 – Ashutosh 2012-04-15 22:12:38

0

我会做的UITableViewCell的子类,都有自己的操作方法接收按钮点击,并用一个块来处理的点击,使您的控制器代码可能看起来是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    SongCell *cell = [tableView dequeueReusableCellWithIdentifier:[SongCell identifier]]; 
    if (cell == nil) { 
     cell = [[SongCell alloc] init];   
    } 
    cell.songData = [dataToDisplay objectAtIndex:indexPath.row]; 
    cell.onPreviewClick = 
    ^
    (SongCell *cell) 
    { 
     cell.isPlaying = YES; 
     [self playSongAtIndexPath:indexPath]; // method to start the preview 
    }; 
    return cell 
} 

为了支持这一点,你的UITableViewCell子类会是这个样子:

// SongCell.h 
@interface SongCell : UITableViewCell 
@property (nonatomic, strong) NSArray *songData; // NSDictionary would be better 
@property (nonatomic, copy) void(^onPreviewClick)(SongCell *cell); 
@property (nonatomic, assign) BOOL isPlaying; 
+ (NSString *)identifier; 
@end 

与此实现:

// SongCell.m 
#import "SongCell.h" 
@interface SongData() 
@property (nonatomic, strong) NSArray *_songData; 
@property (nonatomic, assign) BOOL _isPlaying; 
@property (nonatomic, strong) UIButton *playImage; 
@property (nonatomic, strong) UIButton *pauseImage; 
@property (nonatomic, strong) UIButton *previewButton; 
@property (nonatomic, strong) UILabel *songNameLabel; 
- (void)previewButtonPressed:(UIButton *)button; 
@end 

@implementation SongCell 
@synthesize onPreviewClick, _songData, _isPlaying, playImage, pauseImage, previewButton, songNameLabel; 

- (id)init { 
    if ((self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[SongCell identifier]])) { 
     self.selectionStyle = UITableViewCellSelectionStyleNone; 

     self.playImage = [UIImage imageNamed:@"music_player_play_button.png"]; 
     self.pauseImage = [UIImage imageNamed:@"music_player_pause_button.png"]; 
     self.previewButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     previewButton.frame = CGRectMake(11,6, 36, 35); 
     [previewButton addTarget:self 
          action:@selector(previewButtonPressed:) 
       forControlEvents:UIControlEventTouchUpInside]; 
     [self addSubview:previewButton]; 

     self.songNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,15, 150, 15)]; 
     songNameLabel.font = [UIFont boldSystemFontOfSize:12]; 
     [self addSubview:songNameLabel]; 
    } 
    return self; 
} 

+ (NSString *)identifier { 
    return @"SongCell"; 
} 

- (void)setSongData:(NSArray *)songData 
{ 
    self._songData = songData; 
    [self updateSubviews]; 
} 

- (NSArray *)songData { 
    return _songData; 
} 

- (void)setIsPlaying:(BOOL)isPlaying { 
    self._isPlaying = isPlaying; 
    [self updateSubviews]; 
} 

- (BOOL)isPlaying { 
    return _isPlaying; 
} 

- (void)previewButtonPressed:(UIButton *)button { 
    if (onPreviewClick) onPreviewClick(self); 
} 

- (void)updateSubviews { 
    if (_isPlaying) { 
     [previewButton setBackgroundImage:pauseImage forState:UIControlStateNormal]; 
    } 
    else { 
     [previewButton setBackgroundImage:playImage forState:UIControlStateNormal]; 
    } 
    songNameLabel.text = [_songData objectAtIndex:5]; 
} 

@end 

警告:我没有测试这个。希望它没有太多的错误。

+0

这一切都已完成,我都准备提交这个应用程序,并且不希望在此课程中有任何特别的变化。有没有做子类的方法。 – Ashutosh 2012-04-15 23:40:18

相关问题