2012-04-15 131 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

相關問題