【问题标题】:How to change a button image after selecting a row from table view - iOS从表格视图中选择一行后如何更改按钮图像 - iOS
【发布时间】:2013-06-28 22:33:30
【问题描述】:

我是 Xcode 和 iOS 开发的新手,我正在尝试构建一个播放少量 mp3 文件的应用程序。 The corresponding file is played when a row in a tableview is selected.我用PlayButton Icon 创建了一个播放按钮(自定义按钮)。我在这里要做的是:

  1. 当我选择一行时,歌曲会播放,然后图像应该从“播放”变为“暂停”
  2. 到目前为止,我已经能够对播放按钮进行编程,这样如果歌曲正在播放,按钮图像就会切换到暂停/播放。

我需要帮助的是:

  1. 如何从didSelectRowAtIndexPath 访问自定义按钮
  2. 如何将图像更改为其他图像。

任何帮助都会非常有帮助。

【问题讨论】:

  • 您是否尝试访问标准UITableViewCell 中内置的UIImageView
  • 我创建的按钮在表格视图之外。这是我为我的应用程序创建的一种工具栏,其中包含播放/下一个/上一个按钮。 (类似于任何音乐应用)。
  • 此信息并非不重要,因为听起来按钮在单元格中^^所以我的回答在这里没用:P
  • @user2539989,为什么不直接对按钮使用IBOutlet 属性,然后像“self.myButton”一样引用它?

标签: ios xcode image button tableview


【解决方案1】:

引用任何静态(不在单元格上)视图(例如 UIButton)的标准(且简单)是在 XCode 中启用助手编辑器,然后按住 CTRL 键将项目从 Interface Builder 拖到 .h 文件的接口部分.

这将为项目创建一个IBOutlet 属性。 XCode 会提示你命名属性。

如果您将 IBOutlet 命名为“playButton”,那么您可以像 self.playButton 一样从同一个视图控制器中引用它。

【讨论】:

    【解决方案2】:

    我建议创建一个自定义单元格,以便每个单元格都可以自己管理它的项目。一个简单的类,它继承自 UITableViewCell 并保存歌曲的实体、UIButton 和一些方法。 播放/暂停状态可能还有一个BOOL 值。

    // CustomCell.h
    @interface CustomCell : UITableViewCell
    {
        IBOutlet UIButton *playButton;
        id songEntity;
        BOOL isPlaying;
    }
    
    -(void)playSong;
    -(void)pauseSong;
    
    @end
    
    
    // CustomCell.m
    #import "CustomCell.h"
    
    @implementation CustomCell
    
    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    {
        if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) 
        {
            isPlaying = NO;
            // init button...
            UIImage *img = [UIImage imageNamed:@"playButton.png"];
            [playButton setBackgroundImage:img forState:UIControlStateNormal];
        }
        return self;
    }
    
    -(void)playSong
    {
        // ...
        UIImage *img = [UIImage imageNamed:@"pauseButton.png"];
        [playButton setBackgroundImage:img forState:UIControlStateNormal];
    }
    
    -(void)pauseSong
    {
        // ...
        UIImage *img = [UIImage imageNamed:@"playButton.png"];
        [playButton setBackgroundImage:img forState:UIControlStateNormal];
    }
    
    //...
    @end
    

    【讨论】:

    • 好的。非常感谢。我会尝试这两种解决方案。感谢你的帮助。到目前为止,这对我来说是构建应用程序逻辑中最困难的部分。 :)
    • 这是我尝试过的,它成功了: UIButton *playBtn = (UIButton *)[self.view viewWithTag:10]; [playBtn setImage:[UIImage imageNamed:@"PauseButton.png"] forState:UIControlStateNormal];
    【解决方案3】:

    回答你的部分

    1 ) 为您的自定义按钮分配一个标签,比如说“10”

    现在在您的 didSelectRowAtIndexPath 上尝试类似的操作

    UITableViewCell *cell =  [tableView cellForRowAtIndexPath:indexPath];
    UIButton *playBtn = (UIButton*)[cell viewWithTag:10];   //This way you can acess your custom button
    

    2) 分配/更改图像很简单,方法如下

        [playbtn setImage:[UIImage imageNamed:@"name of your image"] forState:UIControlStateNormal];
    

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2015-04-21
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多