2014-09-01 2 views
0

У меня есть два массива. Один со студенческими деталями и другие с деталями персонала. Эти данные в массиве получаются путем изменения сегментов в контроллере представления. И я хочу отображать эти массивы в двух разделах в виде содержимого ячейки. Как я могу это сделать?Отображение содержимого в виде таблицы в виде ячеек в секции

ответ

0

Вот простой рабочий реализация того, что я думаю, что вы ищете:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // here is the staff and students array for demonstration purposes 
    self.staff = @[@"Mr. Jones", @"Mrs. Smith", @"Principal Alfred", @"Miss Agnes"]; 
    self.students = @[@"Timmy", @"Joey", @"Suzie"]; 
} 


#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // there will be two sections, the staff array in the first section 
    // and the student array for the second section 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // if this section is the first section, return the number of cells as there 
    // are staff entries in the array, otherwise this is the second section so we 
    // can return the number of students in the array 
    return section == 0 ? self.staff.count : self.students.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // try to reuse a cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    // if we had no cells to reuse, create one 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 
    } 

    // now we can customize the cell based on the index path. If this is the first section 
    // we want to display the staff information, and if this is the second section we want to display 
    // the student information, but in both cases we use the row to figure out which entry in each 
    // array we want to use. Assuming the arrays are filled with name strings, we can just set the 
    // textLabel's text to the name for this section/row 
    NSString *name = indexPath.section == 0 ? self.staff[indexPath.row] : self.students[indexPath.row]; 
    cell.textLabel.text = name; 

    // then return the cell 
    return cell; 
} 

@end 

enter image description here

Только в случае, если вы не знакомы с поточных условными, два точно те же:

// this means return the count of the staff array if the section is 0, otherwise return the 
// count of the student array, just like the if-statement below 
return section == 0 ? self.staff.count : self.students.count; 

// this is the same as the in-line conditional above, and while less concise and compact, 
// it is more likely understood in many cases 
if (section == 0) { 
    return self.staff.count; 
} 
else { 
    return self.students.count; 
} 
+1

Спасибо, Майк. Это то, что я ищу. Я думаю, что это лучший ответ. Но я не выделяю массив подробностями. Я просто получаю содержимое массива как пользовательские входы с другого контроллера представления. – user3815344

+0

Итак, вы понимаете, что делать или вам нужна помощь? – Mike

+0

Да. Я знаю, что делать дальше. Спасибо за вашу помощь. Если я нажимаю на изображение, как сохранить его в массиве. И если я выберу другое изображение, я хочу заменить старое изображение. Не могли бы вы мне помочь? – user3815344

Смежные вопросы