我是初学者,我对NSArray有些问题。
我创建了一个数组并将一个新对象(dataclass)添加到locationArray
@synthesize UniqueID,name, address, latitude, longitude, type;
+ (id)LocationWithID:(NSString *)UniqueID name:(NSString *)name address:(NSString *)address latitude:(NSString *)latitude longitude:(NSString *)longitude type:(NSString *)type
{
agilentLocation *newLocation = [[self alloc] init];
newLocation.UniqueID = UniqueID;
newLocation.name = name;
newLocation.address = address;
newLocation.latitude = latitude;
newLocation.longitude = longitude;
newLocation.type = type;
return newLocation;
}
NSMutableArray *LocationArray;
[LocationArray addObject:[dataclass LocationWithID:UniqueID name:name address:address latitude:latitude longitude:longitude type:type]];
我想问一下如何调用我的dataclass下的值。例如,我需要在我的表标题的数据类下调用该名称。
通常,当我正在建造一张桌子时,我会使用
cell.textLabel.text = [LocationArray objectAtIndex:indexpath.row];
但在这种情况下,只有一个对象是“dataclass”,但它下面有几个对象。 我应该如何在桌子上显示名称。
以下代码用于打印对象(数据类)的值
for (dataclass *theLocation in LocationArray) {
NSLog(@"Name: %@ Address: %@ Latitude: %g Longitude: %g Type :%@", theLocation.name, theLocation.address ,[theLocation.latitude doubleValue], [theLocation.longitude doubleValue], theLocation.type);
}
答案 0 :(得分:0)
// First Return The Number of Sections.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)iTableView {
NSInteger aSectionCount = 0;
return aSectionCount;
}
// Now return the No Of Rows. In your case the total count of DataClass in LocationArray
- (NSInteger)tableView:(UITableView *)iTableView numberOfRowsInSection:(NSInteger)iSection {
NSInteger aRowCount = 0;
aRowCount = [LocationArray count];
return aRowCount;
}
// Now for the respective Row, Pick the DataClass from Location Array and display the name.
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
NSInteger theRow = iIndexPath.row;
NSInteger theSection = iIndexPath.section;
NSString *aCellIdentifier = [NSString stringWithFormat:@"Cell-%d-%d-%d", theSection, theRow, iTableView.tag];
UITableViewCell *aCell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:aCellIdentifier];
if (aCell == nil) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aCellIdentifier] autorelease];
}
dataclass *theLocation = [LocationArray objectAtIndex:theRow];
aCell.TitleLabel.Text = theLocation.name;
return aCell;
}
请注意,LocationArray应该是一个类变量。公共或私人,基于您的便利。
希望这就是你要找的......