Не могу заполнить ячейки массивом, когда я загрузил второй UITableViewController - PullRequest
0 голосов
/ 01 апреля 2010

Я очень новичок в программировании для iPhone, я создаю свое первое приложение (кубок мира) Первый вид - это вид таблицы. текстовая метка ячейки заполнена массивом, поэтому она показывает все группы (группы a, B, c и т. д.), затем, когда вы выбираете группу, она использует другой UITableViewcontroller, но что бы я ни делал, я не могу установить текстовую метку Ячейки (например, Франция, Мексика, Южная Африка и т. д. фактически ничего, что я делаю с cellForRowAtIndexPath, не имеет значения, может кто-нибудь сказать мне, что я делаю неправильно, пожалуйста

Спасибо

Вот мой код для контроллера вида:

GroupADetailViewController.m

#import "GroupADetailViewController.h"

@implementation GroupADetailViewController

@synthesize groupLabel = _groupLabel;
@synthesize groupADetail = _groupADetail;
@synthesize teamsInGroupA;

#pragma mark Memory management
- (void)dealloc {
    [_groupADetail release];
    [_groupLabel release];
    [super dealloc];
}

#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the number label to show the number data
    teamsInGroupA  = [[NSArray alloc]initWithObjects:@"France",@"Mexico",@"Uruguay",@"South Africa",nil];
    NSLog(@"loaded");
    // Set the title to also show the number data
    [[self navigationItem]setTitle:@"Group A"];

    //[[self navigationItem]cell.textLabel.text:@"test"];

    //[[self navigationItem] setTitle[NSString String
}

- (void)viewDidUnload {
    [self setgroupLabel:nil];
}

#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    // Return the number of sections in the table view
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in a specific section
    // Since we only have one section, just return the number of rows in the table
    return 4;
    NSLog:("count is %d",[teamsInGroupA count]);
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    static NSString *cellIdentifier2 = @"Cell2";

    // Reuse an existing cell if one is available for reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];

    // If no cell was available, create a new one
    if (cell == nil) {
        NSLog(@"no cell, creating");
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    NSLog(@"cell already there");

    // Configure the cell to show the data for this row
     //[[cell textLabel]setText:[NSString string

    //[[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    //NSUInteger row = [indexPath row];


    //[cell setText:[[teamsInGroupA objectAtIndex:indexPath:row]retain]];
    //cell.textLabel.text:@"Test"   
    [[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    return cell;
}


@end

GroupADetailViewController.m

#import "GroupADetailViewController.h"

@implementation GroupADetailViewController

@synthesize groupLabel = _groupLabel;
@synthesize groupADetail = _groupADetail;
@synthesize teamsInGroupA;

#pragma mark Memory management
- (void)dealloc {
    [_groupADetail release];
    [_groupLabel release];
    [super dealloc];
}

#pragma mark View lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the number label to show the number data
    teamsInGroupA  = [[NSArray alloc]initWithObjects:@"France",@"Mexico",@"Uruguay",@"South Africa",nil];
    NSLog(@"loaded");
      // Set the title to also show the number data
    [[self navigationItem]setTitle:@"Group A"];

    //[[self navigationItem]cell.textLabel.text:@"test"];

    //[[self navigationItem] setTitle[NSString String
}

- (void)viewDidUnload {
    [self setgroupLabel:nil];
}

#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    // Return the number of sections in the table view
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in a specific section
    // Since we only have one section, just return the number of rows in the table
    return 4;
    NSLog:("count is %d",[teamsInGroupA count]);
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    static NSString *cellIdentifier2 = @"Cell2";

    // Reuse an existing cell if one is available for reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];

    // If no cell was available, create a new one
    if (cell == nil) {
        NSLog(@"no cell, creating");
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2] autorelease];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    NSLog(@"cell already there");

    // Configure the cell to show the data for this row
    //[[cell textLabel]setText:[NSString string

    //[[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    //NSUInteger row = [indexPath row];


    //[cell setText:[[teamsInGroupA objectAtIndex:indexPath:row]retain]];
    //cell.textLabel.text:@"Test"   
    [[cell textLabel]setText:[teamsInGroupA objectAtIndex:indexPath.row]];
    return cell;
}

@end

Ответы [ 3 ]

0 голосов
/ 01 апреля 2010

Ваш tableViewController должен установить для свойства делегата экземпляр, соответствующий UITableViewDelegate, а для свойства dataSource - объект, соответствующий протоколу UITableViewDataSource. Конечно, эти свойства могут быть установлены на себя: Вы можете вставить это

self.dataSource = self;
self.delegate = self;

в вашем методе viewDidLoad.

Если вы этого не сделаете, требуемые методы (определенные в протоколах), которые вы реализовали, никогда не будут вызваны, потому что эти два свойства по умолчанию равны нулю. Это исправит твою проблему?

0 голосов
/ 19 июня 2015

Этот cocoapod https://github.com/xmartlabs/XLData делает загрузку UITableView / UICollectionView намного проще, чем прямая работа с dataSource и делегатами.

0 голосов
/ 01 апреля 2010

Правильно ли вы устанавливаете делегата на втором UITableView? Это загружается из XIB или вы просто жестко его кодируете? Кроме того, я не вижу didSelectCellForIndexPath: нигде. Второй tableView даже загружается на экран?

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