Last active
June 15, 2016 11:54
-
-
Save paulw11/3dc18c3844df100863033f41890e532c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// AdTableTest | |
// | |
// Created by Paul Wilkinson on 15/06/2016. | |
// Copyright © 2016 Paul Wilkinson. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { | |
enum CellType: String { | |
case contentCell | |
case advertisingCell | |
} | |
@IBOutlet var tableView: UITableView! | |
let kAdFrequency = 3 | |
let kNumberRows = 25 | |
var tableContent = [String]() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
for i in 1...kNumberRows { | |
self.tableContent.append("Row \(i)") | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func numberOfSectionsInTableView(tableView: UITableView) -> Int { | |
return 1 | |
} | |
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
let adCount = self.adCountForRowCount(self.tableContent.count, frequency: kAdFrequency) | |
return self.tableContent.count + adCount | |
} | |
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cellType = self.cellTypeForRow(indexPath.row, baseRowCount: self.tableContent.count, frequency: self.kAdFrequency) | |
let cell = tableView.dequeueReusableCellWithIdentifier(cellType.rawValue) | |
switch cellType { | |
case .contentCell: | |
let contentRow = contentRowsWithAds(indexPath.row, frequency: kAdFrequency) | |
cell!.textLabel!.text = self.tableContent[contentRow] | |
case .advertisingCell: | |
cell!.textLabel!.text = "Advertisement" | |
} | |
return cell! | |
} | |
func cellTypeForRow(row: Int, baseRowCount: Int, frequency: Int) -> CellType { | |
var cellType = CellType.contentCell | |
let computeRow = row+1 | |
let contentRows = contentRowsWithAds(row, frequency: frequency) | |
if frequency > baseRowCount && row >= baseRowCount { | |
cellType = .advertisingCell | |
} else if row != 0 && computeRow % (frequency+1) == 0 { | |
cellType = .advertisingCell | |
} else if contentRows >= baseRowCount { | |
cellType = .advertisingCell | |
} | |
return cellType | |
} | |
func adCountForRowCount(rowCount: Int, frequency: Int) -> Int { | |
var adCount = rowCount / (frequency) | |
if adCount == 0 { | |
adCount = 1 | |
} | |
return adCount | |
} | |
func contentRowsWithAds(row: Int, frequency: Int) -> Int { | |
let numberOfAds = row / (frequency+1) | |
return row - numberOfAds | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(int) calculatedIndexForRow:(int)row adFrequency:(int)frequency { | |
int numberOfAds = row / (frequency+1) | |
return row - numberOfAds | |
} | |
int calculatedIndex = [self calculatedIndexForRow:indexPath.row adFrequency:kAdCellFrequency]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment