Я внедряю AdMob в UITableView
, помещая баннер в первую строку раздела. Я больше всего использую его, но мне трудно получить cellForRowAtIndexPath
для работы по своему желанию.Программно изменяющий NSIndexPath в Swift
Это то, что мой numberOfRowsInSection
выглядит следующим образом:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = Int()
if let sections = fetchedResultsController.sections {
let currentSection = sections[section]
count = currentSection.numberOfObjects
count = count + 1 // add another row for an ad
}
return count
}
Мои cellForRowAtIndexPath
выглядит следующим образом:
override func tableView(tableView: UITableView, var cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let adCell: BannerAdTableViewCell = tableView.dequeueReusableCellWithIdentifier(BannerAdTableViewCell.reuseIdentifier(), forIndexPath: indexPath) as! BannerAdTableViewCell
// customization
return adCell
} else {
// Cell for vanilla item to display
// TODO: fix indexpath here. need to add 1
let newIndexPath = indexPath.indexPathByAddingIndex(indexPath.row+1)
indexPath = newIndexPath
// Cell for a Routine
let customCell = tableView.dequeueReusableCellWithIdentifier(RoutineSelectionTableViewCell.reuseIdentifier(), forIndexPath: indexPath) as! RoutineSelectionTableViewCell
let routine = fetchedResultsController.objectAtIndexPath(indexPath) as! SavedRoutines
customCell.routineNameLabel.text = routine.routineTitle
return customCell
}
}
Я знаю, что нужно отрегулировать значение indexPath
для учета дополнительной строки в indexPathSection
, но все, что я пробовал, запускает исключения из-за каких-то исключений. Любая обратная связь будет принята с благодарностью.
Что произойдет, если вы удалите следующие строки 'let newIndexPath = indexPath.indexPathByAddingIndex (indexPath.row + 1) indexPath = newIndexPath' и использовать фактический индекс? –