如何将 indexPath.row 设置为要在 NSTimer 选择器函数中使用的表视图函数之外的变量

How to set the indexPath.row as a variable outside tableview functions to be used in the NSTimer selector function

或如何在 TableViewController 文件的 tableview 函数中 运行 NSTimer 选择器函数?我还是新手,所以请多多包涵。

我正在尝试使用 NSTimer 每半秒连续更改一次图像来为 TableViewController 上的图像制作动画。

在我的理解中,计时器需要一个选择器,这是一个将计数器变量设置到单元格图像中的函数。

但我似乎无法在 tableView 函数之外获得 indexPath.row,也无法 运行 NSTimer 函数,因为选择器。

这是我的代码:

import UIKit

class tableViewController: UITableViewController {


override func viewDidLoad() {
    super.viewDidLoad()
  ....
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var myCell:cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as cell

    var partySprites = array[indexPath.row]["partySprite"] as? String

    // The fixed 1 should be the dynamic variable as seen in the result function
    let image = UIImage(named: "sprites/\(partySprites!)1.png")
    myCell.partySprite.image = image

//        var timer = NSTimer()
//        timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("result"), userInfo: nil, repeats: true)

        return myCell
    }

}

这就是我想在结果函数中做的事情:

        func result() {
            counter++
            if counter == 3 {
                counter = 1
            }

            let image = UIImage(named: "sprites/\(partySprites!)\(counter).png")
            myCell.partySprite.image = image

        }

但不能,因为 indexPath.row 在 tableView 函数之外不存在,也不能在 tableView 函数内部调用此函数。

谢谢!

编辑:我还有以下 class,它有助于识别来自 table 视图单元格的图像,在声明 myCell[ 时也用于 tableViewController

class cell: UITableViewCell {

@IBOutlet var partySprite: UIImageView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

您必须实现 tableview 的委托和数据源,它们需要您必须配置的三个方法。

你可以参考我的代码。我已经测试了我的项目,它可以工作

注意:您必须将 tableviewcell 的标识符设置为 "myIdentifier",这是我在代码中设置的。如果您不知道在哪里设置,请转到 attribute inspector(项目的右上角),识别 属性。而且我有很多图片,他们的名字从"ad1.png"到"ad100.png",你也必须改变你的图片名称。

import UIKit

class ViewController: UITableViewController {

    var aTimer:NSTimer?
    var increcementCount:Int = 1
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        //init a NSTimer
        aTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "increase", userInfo: nil, repeats: true)
    }
    //this method was called every 0.5 second
    func increase() {
        increcementCount += 1
        self.tableView.reloadData()
    }
    //tableview datasource
    override func tableView(tableView: UITableView, sectionForSectionIndexTitle title: String, atIndex index: Int) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    //tableview delegate
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier = "myIdentifier"
        var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = "ok"
        //set your image
        cell.imageView?.image = UIImage(named: String(format: "%@%d.png", "ad", increcementCount))
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}