0

Json フィードから入力されたテーブルビューがありますが、行番号は異なります。私がやりたいことは、X 行ごとに広告を挿入することです。たとえば、フィードが 23 行を返す場合、行 5、10、15、および 20 に広告を含む行を挿入します。

現時点では、この問題に対処する最善の方法がわからないため、表示するコードはありません。私が予想する 1 つの問題は、追加する行が 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 に答える