gpt4 book ai didi

ios - UiTableview 每第 n 个结果插入行

转载 作者:行者123 更新时间:2023-11-28 22:29:20 25 4
gpt4 key购买 nike

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

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

感谢任何帮助。

最佳答案

以NSMutableArray为对象插入后者。

int numberofrows;
NSMutableArray *objectsToDisplay;
UITableView *tbleView;

刚刚制作了如下所示的简单表格 View :--

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

您的表格看起来像下面显示的图像:---

enter image description here

关于ios - UiTableview 每第 n 个结果插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com