如何在图像视图上制作数组列表

时间:2015-08-21 08:49:56

标签: ios arrays swift cocoa-touch uiimageview

如何在图像或此程序中的某些内容上制作数组列表,您可以看到此图片以清楚说明。

enter image description here

当我点击其中一张图片时,没有任何问题,它会增加右侧文本框,但是当我滑动并向后滑动时,所有文本框都会变为这样,自动填充。我知道这段代码会让它发生:

qtytext.text = Activitymenu.quantity

但是如何让它成为图片上的数组列表,那么它的地址会增加?

这是我的代码:

struct Activitymenu
{
    static var dataact:Array<AnyObject> = Array <AnyObject>()
    static var quantity: String = ""
}
class CustomCellActivity: UITableViewCell, UITextFieldDelegate {
    @IBOutlet weak var leftlabel: UILabel!
    @IBOutlet weak var myImageView: UIImageView!
    @IBOutlet weak var rightlabel: UILabel!
    @IBOutlet weak var qtytext: UITextField!
    @IBOutlet weak var decrease: UIImageView!
    var dataFilePath: String?
    var value = 0
    var checks:Bool = false
    var dx = 0


    override func awakeFromNib() {
        super.awakeFromNib()
        self.myImageView.userInteractionEnabled = true
        self.decrease.userInteractionEnabled = true
        qtytext.text = Activitymenu.quantity
    }

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

        // Configure the view for the selected state
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        qtytext.resignFirstResponder()
        self.endEditing(true)

    }
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

        let touch: UITouch? = touches.first as? UITouch
        if(touch?.view == myImageView)
        {
            value += 30
            qtytext.text = String(value)

        }
        if(touch?.view == decrease)
        {
            if(value >= 30)
            {
                value -= 30;
                qtytext.text = String(value)

            }

        }
        qtytext.resignFirstResponder()//respon pada editing textbox
        self.endEditing(true)
        var data = Activitiesdata(type: leftlabel.text!, amount: qtytext.text.toInt()!)
        var temp:Activitiesdata
        temp = data
        var idx = 0
        if(temp.amount-30 > 0) {
            temp.amount -= 30

        }
        data = Activitiesdata(type: leftlabel.text!, amount: qtytext.text.toInt()!)
        var json = JSONSerializer.toJson(data)
        var tempJson = JSONSerializer.toJson(temp)

        for(var i = 0; i < Activitymenu.dataact.count; i++){
            if(Activitymenu.dataact[i] as! String == tempJson){
                self.checks = true
                self.dx = i
            }
        }
        if(!self.checks) {
            Activitymenu.dataact.append(json)
        }
        else{
            Activitymenu.dataact[self.dx] = json
        }
        Activitymenu.quantity = String(value)
        NSUserDefaults.standardUserDefaults().setObject(Activitymenu.dataact, forKey: "Aktivitas")
        NSUserDefaults.standardUserDefaults().synchronize();
       println(NSUserDefaults.standardUserDefaults().objectForKey("Aktivitas"))


    }

    func setCell(leftlabeltext: String, rightlabeltext: String, imagename: String)
    {
        self.leftlabel.text = leftlabeltext
        self.rightlabel.text = rightlabeltext
        self.myImageView.image = UIImage(named: imagename)

    }

}

换句话说,我希望他们在我滑动和向后滑动时保留该值。我在我的控制器上使用JSON来获取表格数据

import UIKit

struct JSONActivity
{
    static var jsondata = ""
    static var qty = 0
}


class TableControllerActivities: UITableViewController {

    var items = NSMutableArray()
    var TableData:Array< String > = Array < String >()
    var json:String = ""
    @IBOutlet var myTableView: UITableView!
    var arrayOfMenu: [Activity] = [Activity]()

    @IBAction func setting(sender: AnyObject) {
        self.performSegueWithIdentifier("SettingView", sender: self)
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        let stringKey = NSUserDefaults.standardUserDefaults()

//        println("tes")
        println(stringKey.objectForKey("Aktivitas"))
        self.setUpMenu()
        self.myTableView.delegate = self
        self.myTableView.dataSource = self
//        var stringData: String? = JSONData as? String
//        var json: String = JSONData.parseJSONString

    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrayOfMenu.count //hitung banyak data pada array
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //fungsi agar json dapat membaca tiap row pada storyboard
        var shortDate: String {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MM"
            return dateFormatter.stringFromDate(NSDate())
        }

        let cell: CustomCellActivity = tableView.dequeueReusableCellWithIdentifier("cell") as! CustomCellActivity
        let menu = arrayOfMenu[indexPath.row]
        cell.setCell(menu.type, rightlabeltext: menu.unit, imagename: menu.image)
        //memasukkan data ke json, misal textbox isi 3 maka akan diwrite ke json 3
        var data = Activitiesdata(type: menu.type, amount: String(cell.value).toInt()!)
        var json = JSONSerializer.toJson(data)
        JSONActivity.jsondata = json
        JSONActivity.qty = String(cell.value).toInt()!
//        NSUserDefaults.standardUserDefaults().removeObjectForKey("Aktivitas")
//        NSUserDefaults.standardUserDefaults().synchronize()
//
//      println(NSUserDefaults.standardUserDefaults().objectForKey("Aktivitas"))
        return cell
    }

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

    func do_table_refresh()//mengapdet / merefresh data
    {
        dispatch_async(dispatch_get_main_queue(), {
            self.tableView.reloadData()
        })
    }

    func setUpMenu() //membaca json pada setiap arraynya
    {
        var json: JSON = JSON (data: NSData())
        DataManager.getactivityDataFromFileWithSuccess{ (data) -> Void in
            json = JSON(data: data)
            let results = json["results"]
            for (index: String, subJson: JSON) in results {
            }
            for (var i = 0; i < json["Activity"].count; i++) {
                if let type = json["Activity"][i]["type"].string {
                    //println("Tipe : \(type)")
                    if let icon: AnyObject = json["Activity"][i]["icon"].string {
                        self.items.addObject(icon)
                        if let unit_id = json["Activity"][i]["unit_id"].string {
                            //println("Logo : \(unit_id)")
                            dispatch_async(dispatch_get_main_queue(), {self.tableView!.reloadData()})
                                var menu = Activity(type: type, unit: unit_id, image: icon as! String)
                                    self.arrayOfMenu.append(menu)
                                    self.TableData.append(type + unit_id )
                                    self.do_table_refresh()
                        }
                    }
                }
            }
        }
    }
}

我也在使用NSUserdefaults,但似乎根本不起作用,它会打印所有文本框......

let myquantity = Activitymenu.quantity
        NSUserDefaults.standardUserDefaults().setObject(myquantity, forKey: "SavedStringKey")
        NSUserDefaults.standardUserDefaults().synchronize()
        let stringKey = NSUserDefaults.standardUserDefaults()
        qtytext.text = stringKey.stringForKey("SavedStringKey")

0 个答案:

没有答案