Ad cell after every five cells in tableview

随声附和 提交于 2019-12-14 03:39:54

问题


I need add an ad cell with only image after every five news cells in table view.

If I will add second section for ad cells, an ad cell will be shown after all news cells. If I will add an ad cell when indexPath.row == 4, my fifth news cell won't appear because of cell.details = news[indexPath.row].

Any suggestions how can I do that? Thanks.


回答1:


In the method cellForRowAtIndexPath of UITableViewDataSource do next:

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



回答2:


You need to figure out a system to get the right data for your cells:

0 NEWS

1 NEWS

2 NEWS

3 NEWS

4 NEWS

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

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

You see a system?

You can do something like:

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



回答3:


do like this ..

- (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
}



回答4:


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


来源:https://stackoverflow.com/questions/34282607/ad-cell-after-every-five-cells-in-tableview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!