如何使用swift 2.2将单元格插入集合视图单元格

时间:2017-04-13 11:52:21

标签: ios swift insert uicollectionview uicollectionviewcell

解析api到集合视图现在工作正常我需要插入新的单元格,这也应该执行操作

import UIKit
import Alamofire
import SwiftyJSON
import SDWebImage

 class EditPendingViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UITextViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate{


var favDict = NSMutableArray()





 override func viewDidLoad() {
        super.viewDidLoad()
         JsonParsingFromURL()
       self.navigationController!.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
        FavCollectionView.backgroundColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1.0)
        FavCollectionView.hidden = false
        NoAdsview.hidden = true
        NoAdsview.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1.0)

           JsonParsingFromURL()
        // Do any additional setup after loading the view.
    }

 func JsonParsingFromURL(){
        self.view.makeToastActivity(.Center)
        if Reachability.isConnectedToNetwork() == true
        {
            Alamofire.request(.GET, myapiKeyforsmart + "allad.php?customerid="+MyCustId+"", parameters: nil, encoding: .URL, headers: nil).response { (req, res, data, error) -> Void in
                let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
                print(dataString)
                let json = JSON(data: data!)
                let FavMsg = (json["error_msg"]).stringValue
                self.view.hideToastActivity()
                if error != nil{
                    print("error occured = \(error)")
                }
               else if error == nil && FavMsg == "No favourite available"
                {
                   self.FavCollectionView.hidden = true
                   self.NoAdsview.hidden = false

                }
                else{
                       self.startParsing(data!)
                }
            }
        }
        else{
            self.view.hideToastActivity()
            let alert = UIAlertController(title: "No Internet Connection", message: "make sure your device is connected to the internet", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
    func startParsing(data :NSData)
    {
        let dict: NSDictionary!=(try! NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
        for i in 0  ..< (dict.valueForKey("favourite Ads") as! NSArray).count
        {
            favDict.addObject((dict.valueForKey("favourite Ads") as! NSArray) .objectAtIndex(i))
        }
        FavCollectionView.reloadData()
    }


    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return favDict.count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FavouriteCell", forIndexPath: indexPath) as! ThirdTableViewCell
        let adTitle:NSString = favDict[indexPath.item].valueForKey("adtitle") as! NSString
        let Price:NSString = favDict[indexPath.item].valueForKey("originalprice") as! NSString
        let currencyFormatter = NSNumberFormatter()
        currencyFormatter.usesGroupingSeparator = true
        currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        currencyFormatter.locale = NSLocale.currentLocale()
        let numberFromField = (NSString(string: Price).doubleValue)
        let priceString = currencyFormatter.stringFromNumber(numberFromField)
        let address:NSString = favDict[indexPath.item].valueForKey("districtname") as! NSString
        let ImageString:NSString = favDict[indexPath.row].valueForKey("images") as! NSString
        let parts = ImageString.componentsSeparatedByString(",")
        let firstImage = parts[0]
        cell.FavTitle.text = adTitle as String
        cell.FavPrice.text = priceString! as String
        cell.FavPrice.backgroundColor = UIColor().HexToColor("#8000000", alpha: 0.5)
        cell.FavAddress.text = address as String
        cell.FavImage.sd_setImageWithURL(NSURL(string: imageApikey + (firstImage as String)))
        cell.contentView.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)
        cell.layer.cornerRadius = 10
        cell.layer.borderColor = UIColor.clearColor().CGColor
        if Price == "0"{
            cell.FavPrice.hidden = true
        }
        return cell
    }
 }

这是我在视图控制器中的完整代码。我已经搜索了其他堆栈溢出的答案没有任何帮助我可以帮助我做这一个

0 个答案:

没有答案