代码之家  ›  专栏  ›  技术社区  ›  Abhimanyu

显示横幅广告Objective-C的问题

  •  0
  • Abhimanyu  · 技术社区  · 6 年前

    - (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 回复  |  直到 6 年前
        1
  •  1
  •   Paulw11    6 年前

    您需要增加返回的单元格数 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;
       }
    }