It is not possible to display all the data of one person in one cell, difficulties with indices: (The result is "Unknown" "Sans", but it should be "Unknown" "Persona"

// Псевдо таблица NSArray *person1,*person2; person1 = [NSArray arrayWithObjects:@"Неизвестная",@"персона", nil]; person2 = [NSArray arrayWithObjects:@"Санса",@"Старк", nil]; persons = [NSArray arrayWithObjects:person1, person2, nil]; 

Output code:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{ UITableViewCell *c; c = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; // ячейка UILabel *firstLabel; //? if (c == nil) { c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Hmm"]; } // Передаем выводим через тэг firstLabel = [c.contentView viewWithTag:1001]; firstLabel.text = [persons[0] objectAtIndex:indexPath.row]; UILabel *secondLabel; secondLabel = [c.contentView viewWithTag:1002]; secondLabel.text = [persons[1] objectAtIndex:indexPath.row]; return c; 

}

Label on the left has a tag 1001, Label on the right a tag 1002 enter image description here

enter image description here

    1 answer 1

    The problem is here: secondLabel.text = [persons **[1]** objectAtIndex:indexPath.row]; You create an array that contains two arrays: persons = [NSArray arrayWithObjects:person1, person2, nil];

    Next, you access the second array persons[1] and from there you take the element under the objectAtIndex:indexPath.row cell objectAtIndex:indexPath.row . To get the "Unknown" "Person" you need to go to the first array and get the second element from there: secondLabel.text = [persons[0] objectAtIndex:indexPath.row + 1];

    At the moment, your persons array looks like this: persons = [[Неизвестная][персона],[Санса][Старк]]; where [Неизвестная][персона] has indices [00][01] and [Санса][Старк] [10][11] ;

    • Thanks, but how to use indexPath as an index? In such options, the program drops int firstIndex = indexPath; int secondIndex = 0; UILabel * firstLabel; firstLabel = [c.contentView viewWithTag: 1001]; firstLabel.text = persons [firstIndex] [secondIndex]; secondIndex ++; UILabel * secondLabel; secondLabel = [c.contentView viewWithTag: 1002]; secondLabel.text = persons [firstIndex] [secondIndex]; - StriBog
    • indexPath has two values ​​- section and cell. You cannot do this: int firstIndex = indexPath , int firstIndex = indexPath : int firstIndex = indexPath.row or int firstIndex = indexPath.section all depends on what you need, but judging by your table, you have only one section. - Vitali Eller