表格视图中每五个单元格后的广告单元格

Ad cell after every five cells in tableview

我需要在 table 视图中每五个新闻单元格后添加一个只有图像的广告单元格。

如果我要为广告单元格添加第二部分,则广告单元格将显示在所有新闻单元格之后。 如果我在 indexPath.row == 4 时添加广告单元格,我的第五个新闻单元格将不会出现,因为 cell.details = news[indexPath.row].

有什么建议吗?谢谢

在 UITableViewDataSource 的 cellForRowAtIndexPath 方法中执行下一步:

if (indexPath.row % 5 == 0) {
// configure ad cell
} else {
// configure usual cell with news[indexPath.row - (indexPath.row / 5)]
}

您需要找到一个系统来为您的细胞获取正确的数据:

0 NEWS

1 NEWS

2 NEWS

3 NEWS

4 NEWS

5 AD <----- "5." => (currentRow % 5 == 0)

6 NEWS <----- "6." => (6 - (currentRow / 5)

你看到系统了吗?

您可以这样做:

if ((row % 5) == 0) {
    //Ad stuff here
} else {
    data = tabledata[row - (row/5)];
    //news stuff here using data
}

喜欢这样..

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;//add your own number, might be totalNumberOfNewsItems/5
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
   int reqIndex = indexPath.section*(5) + indexPath.row;
   // get data from array like news[reqIndex];
 }
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  // return your ad view based on section value
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44; // change to your required value
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row % 5 == 0) {
       //configure ad cell
    } else {
       //configure usual cell
    }
}