我是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
}
}
答案 0 :(得分:1)
尝试使用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
}
}