问题
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