[英]How to add multiple Objects to Array - Objective C / iOS
I'm new to programming in Objective C我是 Objective C 编程的新手
Here is my Dilemma: I'm pulling in a JSON file from the web and I'm able to display one of the elements (currDate) to my tableView but now I want to display more.这是我的困境:我正在从 Web 中提取一个 JSON 文件,我能够将其中一个元素 (currDate) 显示到我的 tableView,但现在我想显示更多。 From the code below I would I get it to display both currDate and prevDate从下面的代码我会得到它来显示 currDate 和 prevDate
The logic needs to be changed here:这里需要改一下逻辑:
for (NSDictionary *diction in arrayOfEntry) {
NSString *currDate = [diction objectForKey:@"Current Date"];
a = currDate;
NSString *prevDate = [diction objectForKey:@"Previous Date"];
b = prevDate;
[array addObject:a];
}
[[self myTableView]reloadData];
I'm not sure if I need to change anything here but I'm attaching it to show how I'm displaying the array to my viewTable:我不确定我是否需要在此处更改任何内容,但我将其附加以显示我如何将数组显示到我的 viewTable:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
//cell.textLabel.text = [array objectsAtIndexes:indexPath.row];
return cell;
}
make arrayOfEntry
global.使arrayOfEntry
全局化。 in.h file在.h文件
NSArray *arrayOfEntry;
In numberOfRowsInSection
在numberOfRowsInSection
中
[arrayOfEntry count]
In tableView: cellForRowAtIndexPath
在tableView: cellForRowAtIndexPath
NSString *currDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:@"Current Date"]
NSString *prevDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:@"Previous Date"]
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", currDate, prevDate];
just add another line:只需添加另一行:
[array addObject:a];
[array addObject:b];
To add multiple elements to an Array in objective c you need to use:要将多个元素添加到目标 c 中的数组,您需要使用:
NSMutableArray *newArr = [NSMutableArray new];
Then:然后:
[newArr addObject:dict1];
If you want, you can then set your NSArray
you were using to the NSMutuableArray
after the addition of objects is complete.如果需要,您可以在添加对象完成后将您使用的NSArray
设置为NSMutuableArray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.