Skip to content

Commit c259f0d

Browse files
Add ability to render multiple ads with tableview
1 parent d7cb48c commit c259f0d

File tree

1 file changed

+50
-19
lines changed

1 file changed

+50
-19
lines changed

TeadsSampleApp/Controllers/InRead/Direct/TableView/InReadDirectTableViewController.swift

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ class InReadDirectTableViewController: TeadsViewController {
1515
let contentCell = "TeadsContentCell"
1616
let teadsAdCellIndentifier = "TeadsAdCell"
1717
let fakeArticleCell = "fakeArticleCell"
18-
let trackerViewRowNumber = 3 // tracker view needs to be placed above the slot view
19-
var adRowNumber: Int {
20-
return trackerViewRowNumber + 1
21-
}
18+
var adPosition: [(UUID, Int)] = []
19+
static let startPosition = 3
20+
var adRequestedIndices = Set<Int>()
2221

2322
var placement: TeadsInReadAdPlacement?
2423

@@ -30,6 +29,24 @@ class InReadDirectTableViewController: TeadsViewController {
3029

3130
private var elements = [TeadsElement]()
3231

32+
func trackerViewRowNumber(requestIdentifier: UUID?) -> Int {
33+
guard let requestIdentifier else {
34+
return InReadDirectTableViewController.startPosition
35+
}
36+
guard let position = adPosition.first(where: { uuid, _ in
37+
uuid == requestIdentifier
38+
}) else {
39+
let newPosition = (adPosition.last?.1 ?? 0) + InReadDirectTableViewController.startPosition
40+
adPosition.append((requestIdentifier, newPosition))
41+
return newPosition
42+
}
43+
return position.1
44+
}
45+
46+
func adRowNumber(requestIdentifier: UUID?) -> Int {
47+
return trackerViewRowNumber(requestIdentifier: requestIdentifier) + 1
48+
}
49+
3350
override func viewDidLoad() {
3451
super.viewDidLoad()
3552

@@ -43,10 +60,6 @@ class InReadDirectTableViewController: TeadsViewController {
4360

4461
// keep a strong reference to placement instance
4562
placement = Teads.createInReadPlacement(pid: Int(pid) ?? 0, settings: placementSettings, delegate: self)
46-
placement?.requestAd(requestSettings: TeadsAdRequestSettings { settings in
47-
settings.pageUrl("https://www.teads.com")
48-
})
49-
5063
tableView.register(AdOpportunityTrackerTableViewCell.self, forCellReuseIdentifier: AdOpportunityTrackerTableViewCell.identifier)
5164
}
5265

@@ -57,17 +70,23 @@ class InReadDirectTableViewController: TeadsViewController {
5770
elements.removeAll { $0 == .ad(inReadAd) }
5871
tableView.reloadData()
5972
}
60-
61-
func updateAdCellHeight() {
62-
tableView.reloadRows(at: [IndexPath(row: adRowNumber, section: 0)], with: .automatic)
63-
}
6473
}
6574

6675
extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataSource {
6776
func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
6877
return elements.count
6978
}
7079

80+
func tableView(_: UITableView, willDisplay _: UITableViewCell, forRowAt indexPath: IndexPath) {
81+
if indexPath.row % 3 == 0 {
82+
if elements[indexPath.row] == .article {
83+
placement?.requestAd(requestSettings: TeadsAdRequestSettings { settings in
84+
settings.pageUrl("https://www.teads.com")
85+
})
86+
}
87+
}
88+
}
89+
7190
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
7291
if indexPath.row == 0 {
7392
return tableView.dequeueReusableCell(withIdentifier: contentCell, for: indexPath)
@@ -100,10 +119,16 @@ extension InReadDirectTableViewController: UITableViewDelegate, UITableViewDataS
100119

101120
extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
102121
func didReceiveAd(ad: TeadsInReadAd, adRatio _: TeadsAdRatio) {
103-
elements.insert(.ad(ad), at: adRowNumber)
104-
ad.delegate = self
105-
let indexPaths = [IndexPath(row: adRowNumber, section: 0)]
106-
tableView.insertRows(at: indexPaths, with: .automatic)
122+
let adRowIndex = adRowNumber(requestIdentifier: ad.requestIdentifier)
123+
124+
if adRowIndex <= elements.count {
125+
elements.insert(.ad(ad), at: adRowIndex)
126+
ad.delegate = self
127+
let indexPaths = [IndexPath(row: adRowIndex, section: 0)]
128+
tableView.insertRows(at: indexPaths, with: .automatic)
129+
} else {
130+
print("Invalid index for inserting ad: \(adRowIndex), elements count: \(elements.count)")
131+
}
107132
}
108133

109134
func didFailToReceiveAd(reason: AdFailReason) {
@@ -117,9 +142,15 @@ extension InReadDirectTableViewController: TeadsInReadAdPlacementDelegate {
117142
}
118143

119144
func adOpportunityTrackerView(trackerView: TeadsAdOpportunityTrackerView) {
120-
elements.insert(.trackerView(trackerView), at: trackerViewRowNumber)
121-
let indexPaths = [IndexPath(row: trackerViewRowNumber, section: 0)]
122-
tableView.insertRows(at: indexPaths, with: .automatic)
145+
let trackerRowIndex = trackerViewRowNumber(requestIdentifier: trackerView.requestIdentifier)
146+
if trackerRowIndex <= elements.count {
147+
elements.insert(.trackerView(trackerView), at: trackerRowIndex)
148+
149+
let indexPaths = [IndexPath(row: trackerRowIndex, section: 0)]
150+
tableView.insertRows(at: indexPaths, with: .automatic)
151+
} else {
152+
print("Invalid index for inserting trackerView: \(trackerRowIndex), elements count: \(elements.count)")
153+
}
123154
}
124155
}
125156

0 commit comments

Comments
 (0)