繁体   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