0

在我的UITableViewCell我有一个UIButton和一个标签,它被动态添加到单元格中。我也有单元格的背景图像,因此当单击此按钮时,我希望将单元格的背景替换为另一个图像,或者我希望标签的文本颜色更改为灰色或蓝色等其他颜色。

那可能吗?

这是 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;
}
4

2 回答 2

2

使用标签作为按钮被按下的单元格的标识符。这样您就可以确定需要更改的单元格:

-(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];
}

您应该子类化UITableViewCell,以便可以向其添加新属性并在以后访问它们,就像我对CustomTableViewCell.

是一个关于如何创建自定义的教程UITableViewCell

干杯

于 2012-04-15T21:07:27.103 回答
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

警告:我没有对此进行测试。希望它没有太多错误。

于 2012-04-15T22:22:50.173 回答