Reputation: 247
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
}
}
Upvotes: 1
Views: 105
Reputation: 2859
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
}
}
Upvotes: 1