简体   繁体   English

如何将多个对象添加到数组 - Objective C / iOS

[英]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 numberOfRowsInSectionnumberOfRowsInSection

[arrayOfEntry count]

In tableView: cellForRowAtIndexPathtableView: 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.

相关问题 如何对对象数组进行排序并在iOS Objective c的表视图中显示? - How to sort an array of objects and display in Table View in iOS Objective c? 如何在Objective-C中添加和更新二维数组的对象? - How to add and update the objects of two dimensional array in objective-c? 将UIImageView对象添加到数组(目标C) - Add UIImageView objects to an array (Objective C) Objective-C-如何将多个图像添加到标签上显示的数组? - objective-c - How to add multiple images to an array shown on a label? 如何在Objective C中将两个数组的对象作为对象和键添加到第三个数组 - How to add two array's objects as objects and keys to the third array in Objective C 在目标C中归档多个对象并将其作为对象数组取消归档 - Archiving Multiple Objects and Unarchiving them as array of objects in objective C 如何在地标中添加UIButton? iOS目标C - How to add an UIButton to placemark? ios objective c 如何在iOS目标C的UIImageView中添加UIView - How to add UIView in UIImageView in ios objective c 将对象从数组分配给Google Maps标记-目标C / iOS - assigning objects from array to google maps markers - objective C/iOS Objective-C / iOS:将对象数组转换为JSON字符串 - Objective-C / iOS: Converting an array of objects to JSON string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM