База данных XCode 4.2 NSArray для просмотра таблиц - PullRequest
0 голосов
/ 02 марта 2012

Я новичок, у меня есть проблемы с NSArray.

Я создал массив и добавил новый объект (класс данных) в locationArray

dataclass.m

@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;
}

Table.m

NSMutableArray *LocationArray;

[LocationArray addObject:[dataclass LocationWithID:UniqueID name:name address:address latitude:latitude longitude:longitude type:type]];

Я хотел бы спросить, как я могу назвать значение под моим классом данных.Например, мне нужно назвать имя под классом данных для заголовка таблицы.

Обычно, когда я строю таблицу, я буду использовать

cell.textLabel.text = [LocationArray objectAtIndex:indexpath.row];

, но в этом случае есть только один объект, который является «классом данных», но у него есть несколько объектов под ним.как отображать имя в таблице.

Приведенный ниже код используется для распечатки значений для объекта (класс данных)

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);
}

Снимок экрана

enter image description here

1 Ответ

0 голосов
/ 02 марта 2012
// 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 должен быть переменной класса. Публичный или частный в зависимости от вашего удобства.

Надеюсь, это то, что вы ищете ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...