簡體   English   中英

具有兩個自定義單元格的UITableViewCell-圖像不斷變化

[英]UITableViewCell with two custom cells - Images keep changing

我是iOS編程的新手。 因此,如果您的問題聽起來很幼稚,請提前抱歉。 我在UITableViewCell中有兩個自定義單元格。 一個顯示圖像和標簽,另一個顯示橫幅。 我想在3個單元格中顯示帶有圖像的標簽,然后顯示橫幅,然后繼續。

當前,我能夠根據需要顯示它,但是當我滾動時,圖像和橫幅會更改單元格中的位置。

以下是我的代碼:

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

     }
    }

嘗試使用indexPath確定應使用哪個單元格。 您正在嘗試在第4個,第8個等等的單元格中顯示帶有橫幅的adCell ,因此執行此操作非常簡單:

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

     }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM