Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在tableview中添加了多个图层_Ios_Uitableview_Cocoa Touch_Swift3 - Fatal编程技术网

Ios 在tableview中添加了多个图层

Ios 在tableview中添加了多个图层,ios,uitableview,cocoa-touch,swift3,Ios,Uitableview,Cocoa Touch,Swift3,我试图在tableview上动态设置数据,可以通过滑动删除单元格。单元格创建多个图层,删除后存在某个图层,其显示单元格未删除 我的密码在这里 override func viewDidLoad() { super.viewDidLoad() self.beaconArrayM = NSMutableArray() self.produsctListM = NSMutableArray() self.contentArrayM =

我试图在tableview上动态设置数据,可以通过滑动删除单元格。单元格创建多个图层,删除后存在某个图层,其显示单元格未删除

我的密码在这里

override func viewDidLoad() {
        super.viewDidLoad()

        self.beaconArrayM = NSMutableArray()
        self.produsctListM = NSMutableArray()
        self.contentArrayM = NSMutableArray()


        self.fatchDataFromApiAsync(urlString: videoUrl)//video url
        countBeacons = 0

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillAppear(_ animated: Bool) {

    }


    func fatchDataFromApiAsync(urlString:String) {

        _ = NSArray()
        let url:URL = URL(string: urlString)!

        URLSession.shared.dataTask(with: url){ data,response,error in

            if error != nil {
                print("Data Parsing error = \(error)")
            }
            else{
                do{
                    let jsonResponse      =   try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
                    if jsonResponse["assets"] != nil{
                        self.jsonArray = jsonResponse["assets"] as! NSArray
                        self.HeaderStr = jsonResponse["identifier"] as? String
                        self.navigationController?.navigationBar.topItem?.title = self.HeaderStr

                        self.beaconManager.delegate = self
                        self.swipeTableview.delegate = self
                        self.swipeTableview.dataSource = self
                    }
                }
                catch let error as NSError{
                    print("\(error)")
                }
            }
            }.resume()
    }
}


extension DemoListBeaconsVC: ESTBeaconManagerDelegate
{
    //  :MARK BeaconsManager ranging delegates


    func beaconManager(_ manager: Any, didEnter region: CLBeaconRegion) {

        countBeacons += 1
        if !(region.minor == nil){
            let minorValue = region.minor
            if !(self.beaconArrayM.contains(minorValue!)){
                self.beaconArrayM .add(minorValue!)
            }
            self.contentArrayM = self.beaconArrayM
            self.swipeTableview.reloadData()
        }

    }

    func downloadImage(url: URL,indexPath: IndexPath,productTxt : String) {
        let cell = self.swipeTableview.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SwipeTableViewCell

        print("Download Started")
        getDataFromUrl(url: url) { (data, response, error)  in
            guard let data = data, error == nil else { return }
            print(response?.suggestedFilename ?? url.lastPathComponent)
            print("Download Finished")
            DispatchQueue.main.async() { () -> Void in
                    cell.productImage.contentMode = .scaleAspectFit
                    cell.productImage.image = UIImage(data: data)
                    cell.contentLbl.text = productTxt

            }
        }
    }

    func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _  response: URLResponse?, _ error: Error?) -> Void) {
        URLSession.shared.dataTask(with: url) {
            (data, response, error) in
            completion(data, response, error)
            }.resume()
    }

    func beaconManager(_ manager: Any, didExitRegion region: CLBeaconRegion) {

        countBeacons -= 1
//      if countBeacons == 0{
//      }
        if (((self.beaconArrayM.count)) > 0 && !(region.minor == nil)){
            self.beaconArrayM.remove(region.minor!)
            self.contentArrayM = self.beaconArrayM
            self.swipeTableview.reloadData()
        }
    }


    func beaconManager(_ manager: Any, didDetermineState state: CLRegionState, for region: CLBeaconRegion) {
        switch state {
        case CLRegionState.inside:
            if region == beaconRegion
            {
                self.beaconManager.startMonitoring(for: beaconRegion)
            }
            else if region == beaconRegion
            {
                self.beaconManager.startMonitoring(for: beaconRegion2)
            }
            else if region == beaconRegion
            {
                self.beaconManager.startMonitoring(for: beaconRegion3)
            }

            print("inside REGION",region.description)
            break
        case CLRegionState.outside:
            // Stop Monitoring
            if region == beaconRegion
            {
                self.beaconManager.stopMonitoring(for: beaconRegion)
            }
            else if region == beaconRegion
            {
                self.beaconManager.stopMonitoring(for: beaconRegion2)
            }
            else if region == beaconRegion
            {
                self.beaconManager.stopMonitoring(for: beaconRegion3)
            }
            print("outside REGION",region.description)

            break
        case CLRegionState.unknown:
            print("unknown REGION",region.description)
            break
        }
    }

    func beaconManager(_ manager: Any, didChange status: CLAuthorizationStatus) {

        switch status {
        case .authorizedAlways:
            self.beaconManager.requestAlwaysAuthorization()
            print("authorizedAlways")
            break
        default:
            print("defaultt")
            break
        }
    }

    func beaconManager(_ manager: Any, rangingBeaconsDidFailFor region: CLBeaconRegion?, withError error: Error) {

    }

    func beaconManager(_ manager: Any, didFailWithError error: Error) {

    }

    func beaconManager(_ manager: Any, didStartMonitoringFor region: CLBeaconRegion) {
        if region == beaconRegion
        {
            self.beaconManager.requestState(for: beaconRegion)
        }
        else if region == beaconRegion
        {
            self.beaconManager.requestState(for: beaconRegion2)
        }
        else if region == beaconRegion
        {
            self.beaconManager.requestState(for: beaconRegion3)
        }
    }

    func beaconManager(_ manager: Any, monitoringDidFailFor region: CLBeaconRegion?, withError error: Error) {

    }


}


//: MARK Tableview delegate and datasource

extension DemoListBeaconsVC : UITableViewDataSource, UITableViewDelegate
{

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "Cell"
        //      print("Begin cellForRowAt")
        let cell = self.swipeTableview.dequeueReusableCell(withIdentifier: identifier) as! SwipeTableViewCell

        if (self.contentArrayM.count) > 0 {
            var currentIndex: Int = 0
            let countIndex: Int = 0
            for tempp in self.contentArrayM{
                    let temp = jsonArray .object(at: indexPath.row)
                    self.jsonDic = temp as! NSDictionary
                    self.minor = self.jsonDic["minor"] as? String
                    if let minorInteger = Int(self.minor!) {
                    minorNumber = NSNumber(value:minorInteger)
                    }
                    if minorNumber == tempp as! NSNumber
                    {
                        currentIndex = countIndex
                        break
                    }
                }
//      let temp = jsonArray .object(at: currentIndex)
                let temp = jsonArray .object(at: indexPath.row)
                self.jsonDic = temp as! NSDictionary
                self.minor = self.jsonDic["minor"] as? String
                self.HeaderStr = self.jsonDic["title"] as? String
                self.urlStr = self.jsonDic["url"] as? String
                print("minor",self.minor!)

                if let minorInteger = Int(self.minor!) {
                    minorNumber = NSNumber(value:minorInteger)
                }
            }
            let url = NSURL(string: self.urlStr!)
            downloadImage(url: url as! URL, indexPath: indexPath, productTxt: self.HeaderStr!)
        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return 100.0
    }


    func actionGesture(recognizer: UITapGestureRecognizer)
    {

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        let temp = jsonArray .object(at: indexPath.row)
        self.jsonDic = temp as! NSDictionary
        self.HeaderStr = self.jsonDic["title"] as? String
        self.urlStr = self.jsonDic["url"] as? String

        let demoVC = self.storyboard?.instantiateViewController(withIdentifier: "ProductDetalsVC") as! ProductDetalsVC
        demoVC.productTitle = self.HeaderStr
        demoVC.imageStr = self.urlStr
        self.navigationController?.pushViewController(demoVC, animated: true)
        tableView.deselectRow(at: indexPath, animated: true)
    }


    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

        let delete = UITableViewRowAction(style: .normal, title: "Delete")
        { action, index in
            //          print("delete")
            self.swipeTableview.beginUpdates()
            self.contentArrayM.removeObject(at: indexPath.row)
            self.swipeTableview.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
            print("array count after deletion",self.contentArrayM.count)
//                      self.swipeTableview.reloadRows(at: [indexPath], with: UITableViewRowAnimation.fade)
            self.swipeTableview.endUpdates()
        }
        let done = UITableViewRowAction(style: .default, title: "Done")
        { action, index in
            print("done")
        }
        return [delete, done]
    }


    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // the cells you would like the actions to appear needs to be editable
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

  if editingStyle == .delete {
        print("Deleted")

        self.contentArrayM.remove(at: indexPath.row)
        self.swipeTableview.deleteRows(at: [indexPath], with: .automatic)
  }
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.contentArrayM.count == 0
        {
            return 0
        }
        else
        {
            return self.contentArrayM.count
        }

    }

    func numberOfSections(in tableView: UITableView) -> Int
    {
        return 1
    }


}

我终于找到了答案。 我两次创建tableview单元格,第一次是在CellForRowatineXpath中,第二次是在函数downloadImage()中

现在我将单元格发送到函数downloadImage()中,如下所示

下载图像(cell:cell,url:url!,indepath:indepath,productTxt:self.headerStr!)