【问题标题】:Animated button not visible in UITableViewCellUITableViewCell 中不可见的动画按钮
【发布时间】:2017-09-28 11:44:13
【问题描述】:

我的应用程序在 UITableView 中有一个服务器列表,其中显示您是否使用图像连接到服务器。这目前适用于静态图像。

我现在想添加一个非常基本的动画,显示它正在连接到服务器。只是在“连接”和“断开”图像之间闪烁。

但是当我将按钮的图像设置为动画时,它什么也不显示,没有动画的任何帧,也不是表格单元的默认图像,只是空白。创建这样的动画适用于视图中不在表格中的按钮,因此创建动画不是问题。它确实在按下按钮时开始播放,让我觉得按钮只需要刷新或强制重绘。但是我什么都没找到。

我使用 reloadData 重新加载我的表。

(简体)ServerCell.h的源代码:

@interface ServerCell : UITableViewCell 

    @property (nonatomic, strong) IBOutlet UILabel *nameLabel;
    @property (nonatomic, strong) IBOutlet UILabel *ipLabel;
    @property (nonatomic, strong) IBOutlet UIImageView *serverImageView;
    @property (weak, nonatomic) IBOutlet UIButton *mConnectionBtn;

@end

ServerCell.m

@implementation ServerCell

#pragma mark - Properties

@synthesize nameLabel;
@synthesize ipLabel;
@synthesize serverImageView;
@synthesize mConnectionBtn;

#pragma mark - Constructor/Destructor 

/////////////////////////////////////////////////////////////////
/*
 * Class constructor
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

#pragma mark - Class Methods

/////////////////////////////////////////////////////////////////
/*
 * Communicate to the superview the user selection
 */
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

@end

cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Cast the TableCell to a custom ServerCell
    ServerCell *cell = (ServerCell *)[tableView dequeueReusableCellWithIdentifier:@"ServerCell"];

    [cell.mConnectionBtn addTarget:self action:@selector(ActionServerCell:) forControlEvents:UIControlEventTouchUpInside];

    NSArray* images = [NSArray arrayWithObjects:[UIImage imageNamed:@"connected.png"], [UIImage imageNamed:@"disconnected.png"], nil];
    UIImage* anim = [UIImage animatedImageWithImages:images duration:1.0];

    [cell.mConnectionBtn setImage:anim forState:UIControlStateNormal];

    // return the cell object
    return cell;
}

【问题讨论】:

    标签: ios objective-c uitableview animation uibutton


    【解决方案1】:

    这应该做你想做的事。调用开始动画时可能需要进行调整。

    import UIKit
    
    class ExampleTableViewController: UITableViewController {
    
        var names = ["John", "Will", "Ana"]
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return names.count
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cellIdentifier = "cell"
            var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
    
            if cell == nil {
                cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
                cell?.imageView?.image = UIImage(named: "red")!
                cell?.imageView?.animationImages = [UIImage(named: "red")!, UIImage(named: "green")!]
                cell?.imageView?.animationDuration = 3
                cell?.imageView?.animationRepeatCount = 0
                cell?.imageView?.startAnimating()
            }
            cell!.textLabel?.text = names[indexPath.row]
            return cell!
        }
    }
    

    【讨论】:

    • 我不得不稍微修改一下才能使用我所拥有的。但是仅创建一次动画然后根据我的需要运行 startanimation 或 stopanimation 的概念解决了它。显然将图像再次设置为相同的动画最初破坏了一些东西,仍然不确定为什么,但它现在可以工作了,这对我来说已经足够好了。非常感谢。
    • 没问题 =) 从我的测试中我不得不设置“imageView?.image”和“imageView?.animationImages”。只是设置“imageView?.animationImages”不起作用。很高兴它对你有用。
    【解决方案2】:

    要显示该过程正在运行,您只需添加 activityIndicator 而不是您的图像。

    这可能比设置动画图像更简单。

    1. 将它连接到你的xib:

    @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

    1. 如果你不需要它,只需在你的 cell.m 中设置:

    cell.activityIndicator.hidden = YES;

    1. 然后当您的进程运行时将其打开:

    self.activityIndicator.hidden = NO;

    如果我错了或者我误解了问题,请告诉我:)

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      相关资源
      最近更新 更多