我正在尝试动态设置tableview上的数据,并且可以通过swiping删除单元格。单元格创建多个图层,删除后存在一些图层,其出现的单元格未删除。
我的代码在这里..
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
}
}
答案 0 :(得分:0)
我终于找到了答案。 我创建了tableview单元格两次,第一次在cellForRowAtIndexPath中,第二次在函数downloadImage()中。
现在我将单元格发送到函数downloadImage(),如下所示。
downloadImage(cell:cell,url:url!,indexPath:indexPath,productTxt:self.headerStr!)