不能从另一个视图改变的UIImage - Swift

时间:2014-12-16 22:51:15

标签: ios swift uiimage viewcontroller

我正在开发一款iOS应用程序,用于收集游戏卡。我有一个屏幕,您可以在其中键入卡名称并查看卡片。另一个你可以输入卡片,然后你将在表格视图中(wip,还没有完成)。很难看到表格视图中的图像。因此,当您单击一个单元格时,您将返回到第一个屏幕,在该“图像槽”中,您将看到它更大。但那是不起作用的部分。它是一个标签栏应用程序,两个视图使用相同的viewController(因为这更容易)。当我试图更改图像并在此之后更改视图时,您无法看到它!我不知道问题是什么!这是我的代码:

import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

@IBOutlet var tableView: UITableView!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var cardImage: UIImageView!
@IBOutlet weak var newCardName: UITextField!
@IBOutlet weak var newCardAmount: UITextField!
var selectedIndex = 0
var text = ""

@IBAction func newCardSave(sender: AnyObject) {
    println()
}
var tableData: [String] = ["Brimaz, King of Oreskos", "Belligerent Sliver"]
var tableDataCardAmount: [Int] = [1, 2]

override func viewDidLoad() {
    super.viewDidLoad()

    self.nameTextField?.delegate = self;

    // Register custom cell
    var nib = UINib(nibName: "vwTblCell", bundle: nil)
    tableView?.registerNib(nib, forCellReuseIdentifier: "cell")

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.tableData.count
}

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

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

    var text = tableData[indexPath.row].stringByReplacingOccurrencesOfString(" ", withString: "%20")
    println(text)

    var full_url = "http://mtgimage.com/card/" + text + ".jpg"
    var url = NSURL(string: full_url)
    var image: UIImage?
    var request: NSURLRequest = NSURLRequest(URL: url!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        image = UIImage(data: data)
        cell.imgCarName.image = image
    })
    cell.lblCarName.text = tableData[indexPath.row]
    cell.amountLabel.text = tableDataCardAmount[indexPath.row].description

    return cell

}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("Row \(indexPath.row) selected \(tableData[indexPath.row])")
    self.tabBarController?.selectedIndex = 0
    nameTextField?.text = tableData[indexPath.row]

    let cardName = tableData[indexPath.row]

    text = cardName.stringByReplacingOccurrencesOfString(" ", withString: "%20")

    var full_url = "http://mtgimage.com/card/" + text + ".jpg"
    var url = NSURL(string: full_url)
    var image: UIImage!
    var request: NSURLRequest = NSURLRequest(URL: url!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        image = UIImage(data: data!)
        self.cardImage?.image = image
    })
    println("test")
    println(tableData[indexPath.row])
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 70
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
    self.view.endEditing(true)
    text = nameTextField.text.stringByReplacingOccurrencesOfString(" ", withString: "%20")
    println("text1 " + text)

    var full_url = "http://mtgimage.com/card/" + text + ".jpg"
    println("text2 " + text)
    var url = NSURL(string: full_url)
    var image: UIImage!
    var request: NSURLRequest = NSURLRequest(URL: url!)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
        image = UIImage(data: data!)
        self.cardImage?.image = image
        println("text3 " + self.text)
    })
    return false
}
}

0 个答案:

没有答案