0

我有一个从 Json 提要填充的表格视图,行号各不相同。我想要做的是每 X 行插入一个广告。因此,例如,如果提要返回 23 行,那么我想在第 5、10、15 和 20 行插入一行,其中包含广告。

目前我没有代码可以显示,因为我真的不知道解决这个问题的最佳方法。我预计的一个问题是我添加的行与 Json 提要的源不同,我认为这可能会导致新行出现问题,因为我不想直接修改数据源,或者至少我认为我不。

任何帮助表示赞赏。

4

2 回答 2

2

将 NSMutableArray 作为对象插入后者。

int numberofrows;
NSMutableArray *objectsToDisplay;
UITableView *tbleView;

刚刚制作了简单的表格视图,如下所示:-

- (void)viewDidLoad
{
    tbleView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
    tbleView.delegate=self;
    tbleView.dataSource=self;

    objectsToDisplay=[[NSMutableArray alloc] initWithObjects:@"A ",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];

    numberofrows=[objectsToDisplay count];
    [self.view addSubview:tbleView];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *mycell=[tbleView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (mycell==nil) {
        mycell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }

    mycell.textLabel.text=[objectsToDisplay objectAtIndex:indexPath.row];

    return mycell;
}

要在每 5 个索引后插入行,请使用此方法

-(IBAction)InsertAdvertizes:(id)sender
{
    for (int i=4;i<numberofrows;i=i+5) {
        [objectsToDisplay insertObject:@"Hello I am Advertize" atIndex:i];
    }

    NSLog(@"object is %@",objectsToDisplay);

    [tbleView reloadData];
}

您的表格将如下所示:---

在此处输入图像描述

于 2013-07-29T15:22:35.660 回答
0

使用

tableView: numberOfRowsInSection

设置行的大小

columnsToAdd = round(json.objects.count/5)
return json.objects.count+columnsToAdd

然后在

tableView: cellForRowAtIndexPath

返回您的 objectColumns 或 adColumn

when indexPath.row Modulo 5 != 0 => return yourJsonObjectColumns 
else => return yourAdColumn

我认为这已经足够了。如果您有一些代码,我会再次帮助您。

于 2013-07-29T14:42:07.470 回答