简体   繁体   中英

UITableViewCell with two custom cells - Images keep changing

I am new to iOS programming. So sorry in advance in case my question sounds very naive. I have two custom cells in a UITableViewCell. One displaying images and labels and other displaying banner. I want to display labels with images in 3 cells and then show a banner and this continues.

Currently, I am able to display it as desired but when I scroll, images and banner change positions in cells.

Following is my code:-

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     if VideosTableViewController.flag >= 1 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell

        let  remoteImageUrlString = imageCollection[indexPath.row]
        let imageUrl = NSURL(string:remoteImageUrlString)

        let myBlock: SDWebImageCompletionBlock! = {(image:UIImage!, error: NSError!, cachetype:SDImageCacheType!, imageURL: NSURL!) -> Void in
         }
        //cell.myImageView?.image = nil

        cell.myImageView.sd_setImageWithURL(imageUrl, completed: myBlock)

        //set label
        cell.myImageLabel.text = labelCollection[indexPath.row]
        print(cell.myImageLabel?.text)

        VideosTableViewController.flag = VideosTableViewController.flag - 1

        return cell
          }
     else
     {
        let adCell = tableView.dequeueReusableCellWithIdentifier("adCell", forIndexPath: indexPath) as! VideosBannerAdCustomTableViewCell

        VideosTableViewController.flag = VideosTableViewController.flag + 3

            VideosTableViewController.flag = 3


        adCell.videosBannerView.adUnitID = "banner id" 
        adCell.videosBannerView.rootViewController = self
        let request : DFPRequest = DFPRequest()
       //request.testDevices = [kGADSimulatorID]
        request.testDevices = ["my test device id"] 
        adCell.videosBannerView.loadRequest(request)

       return adCell

     }
    }

Try to use indexPath to determine which cell should be used. You are trying to display adCell with a banner in cell 4th, 8th, .... So it is very simple to be done by this:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

     if indexPath.row % 4 != 0 || indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell

        let  remoteImageUrlString = imageCollection[indexPath.row]
        let imageUrl = NSURL(string:remoteImageUrlString)

        let myBlock: SDWebImageCompletionBlock! = {(image:UIImage!, error: NSError!, cachetype:SDImageCacheType!, imageURL: NSURL!) -> Void in
         }
        //cell.myImageView?.image = nil

        cell.myImageView.sd_setImageWithURL(imageUrl, completed: myBlock)

        //set label
        cell.myImageLabel.text = labelCollection[indexPath.row]
        print(cell.myImageLabel?.text)

        VideosTableViewController.flag = VideosTableViewController.flag - 1

        return cell
          }
     else
     {
        let adCell = tableView.dequeueReusableCellWithIdentifier("adCell", forIndexPath: indexPath) as! VideosBannerAdCustomTableViewCell

        VideosTableViewController.flag = VideosTableViewController.flag + 3

            VideosTableViewController.flag = 3


        adCell.videosBannerView.adUnitID = "banner id" 
        adCell.videosBannerView.rootViewController = self
        let request : DFPRequest = DFPRequest()
       //request.testDevices = [kGADSimulatorID]
        request.testDevices = ["my test device id"] 
        adCell.videosBannerView.loadRequest(request)

       return adCell

     }
    }

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