Проблема с отображением баннерной рекламы Objective-C - PullRequest
0 голосов
/ 03 декабря 2018

У меня проблема при отображении BannerAds для 1-й и 5-й строк.При отображении данных первая строка заменяется баннерной рекламой, а каждая пятая строка заменяется баннерной рекламой ... Как это преодолеть.Вот что я пробовал. TIA

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger n;
    n= [array count];
    return n;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 5 == 0) {
        //configure ad cell

        for(UIView* view in cell.contentView.subviews) {
            if([view isKindOfClass:[GADBannerView class]]) {
                [view removeFromSuperview];
            }
        }
else
{
 Title.text=[NSString stringWithFormat:@"%@ ",[dict objectForKey:@"Name"]];
}
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row % 5 == 0)
        return 60;
    else
        return 153;
}

1 Ответ

0 голосов
/ 03 декабря 2018

Вам необходимо увеличить количество возвращаемых ячеек в numberOfRowsInSection и учесть добавленные строки в cellForRowAt

Количество рекламных объявлений будет 1 + n / 5 (первая строка, а затемкаждая пятая строка), поэтому число ячеек в вашей таблице будет n + n/5 + 1

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger n;
    n= [array count];
    return n/5 + n + 1;
}

Теперь некоторые ячейки, которые вам нужно вернуть из cellForRowAt, будут рекламными, и вам потребуется аккаунтдля этого при доступе к вашему массиву данных.Индекс, который вам нужен, - это номер строки - количество рекламных строк, предшествующих этому.Это индекс / 5 + 1 (первая строка и каждые 5 строк).

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 5 == 0) {
        AdCell *cell = (AdCell *)[tableView dequeueReusableCellWithIdentifier:"Ad" forIndexPath: indexPath];
        ...
        NSLog(@"Showing an ad at row %ld",indexPath.row);
        return cell;
    else
    {
        NSInteger index = indexPath.row - indexPath.row/5 - 1;
        NSDictionary *dict = myArray[index];
        NormalCell *cell = (NormalCell *)[tableView dequeueReusableCellWithIdentifier:"Normal" forIndexPath: indexPath];
        cell.title.text=[NSString stringWithFormat:@"%@ ",dict["Name"]];
        NSLog(@"Showing a normal row at row %ld (data from element %ld of array)",indexPath.row,index);
        return cell;
   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...