简体   繁体   中英

Custom UITableView sections being added more than once

I've made a custom class which adds a section header title and section content to a UITableViewCell. I've managed to get the sections to show up but each section is added 5 times for some strange reason.

Section Items Class

  class MainSectionItems: SectionItems {
            override init() {
                //super.init()
            }
        }
        class SectionItems:NSObject{
            var sections:[String] = []
            var artists:[[String]] = []
            var songs:[[String]] = []
            var imgUrl: [[String]] = []
            func addSection(section: String, artist:[String], song: [String], imageUrl: [String]){
                sections += [section]
                artists = artists + [artist]
                songs = songs + [song]
                imgUrl = imgUrl + [imageUrl]
            }
        }

MainViewController

    override func viewDidLoad() {
       getData{
                (msg) in
                self.menuItems.addSection("Featured Post", artist: self.artistArray, song: self.songNameArray, imageUrl: self.imgUrls)
              self.tVewCell.reloadData()
                self.collectionView.reloadData()
                    SwiftSpinner.hide()
                }
}

getData is a method which retrieves 5 objects from parse.com geData Function

func getData ( completionHandler: (String) -> ()) -> () {
        let query = PFQuery(className:"chordBankA")
        query.orderByDescending("Artist")
        query.limit = 5
        query.findObjectsInBackgroundWithBlock {
            (objects: [PFObject]?, error: NSError?) -> Void in
            if error == nil {
                for object in objects! {
                    self.artistArray.append((object.objectForKey("Artist")) as! String)
                    self.songNameArray.append(((object.objectForKey("Song")) as! String))
                    self.imgUrls.append(((object.objectForKey("imgUrl")) as! String))
        completionHandler("0");
                }
            } else {
                print("Error: \(error!) \(error!.userInfo)")
                completionHandler("1");
            }
        }

Thank you for sharing getData code. Your issue is that you are calling your completion block inside for loop. Put it outside for loop and you should be good!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM